S6 中引入了箭頭函數,這也是現在前端面試幾乎必考的內容(沒考箭頭函數,我都不好意思說自己是面試官,哈哈,開個玩笑)。有人問我,箭頭函數是個什么東西?我跟他說,就像Java和C#中的lambda。
let func = (s)=> { console.log(s); };
func("hello world");
interface Operate {
void doSomething(String str);
// void doSomething1(); 不可以有兩個方法
}
public static void main(String[] args) {
Operate func = (String s)->{ System.out.println(s);};
func.doSomething("hello world");
}
var func = (string s)=> { Console.WriteLine(s); };
func("hello world");
可以看到,寫法非常類似,尤其是Js和C#。 變量func可以被當做一個函數來使用。
那么用于承接這個匿名方法的變量實際是什么?
JavaScript: 就是一個js中的function
Java: 在例子中,有點容易迷惑,明明是將lambda賦值給了一個接口類型。但最終調用的時候又要調用該接口的doSomething方法。而且這個接口只能有一個對應的方法,多了會報錯。
Java10中也提供了var關鍵字,但遺憾的是也不能被用于這樣lambda賦值的情況。
C#: 實際上是一個委托類型,例如:
delegate void doSomething(string str);
public static void Main(string[] args) {
doSomething func = (string s) => { Console.WriteLine(s); };
func("hello world");
}
這樣看和Java有點像了,但定義的仍然是一個方法,而不是一個接口中有一個同樣類型的方法。
如果在c語言中我們會用一個指向函數的指針。
在上一節的例子中,“hello world”是以參數的形式傳遞到方法中的,那么,是否可以直接引用外部的方法呢?
當然是可以的,改造一下上面的例子:
let str = ",圣誕快樂。";
let func = (s)=> {
console.log(s + str);
str = ",春節快樂。"
};
str = ",元旦快樂。"
func("hello world");
func("hello world");
interface Operate {
void doSomething(String str);
// void doSomething1(); 不可以有兩個方法
}
public static void main(String[] args) {
final String str = ",圣誕快樂";
Operate func = (String s)->{
System.out.println(s + str);
//str = ",春節快樂。";
};
//str = ",元旦快樂。"
func.doSomething("hello world");
}
var str = ",圣誕快樂。";
var func = (string s) => {
Console.WriteLine(s + str );
str = ",春節快樂。";
};
str = ",元旦快樂。";
func("hello world");
func("hello world");
hello world,元旦快樂。
hello world,春節快樂。
可見,在函數執行的時候,會取當時str的值。在函數定義的時候,雖然引用了變量str,但不是此時固定了str的值。
在函數中改變了str的值,會改變外部str的值。
Java的例子中,要求str是final的才行,所以是無法對str改變的。
在JavaScript中,經常會用到類似callback的回調方法,那么箭頭函數是不是也可以呢?
let func = (s)=> {
console.log(s);
};
var showLog = function(str,action){
action(str);
}
showLog("hello world",func);
本例用Consumer代替了第一節中的自定義的Operate接口。其實Consumer就是框架幫我們預定義的泛型接口,避免我們總需自定義一個接口:
public static void main(String[] args) {
Consumer<String> func = (String s)->{
System.out.println(s);
};
showLog("hello world",func);
}
public static void showLog(String str, Consumer<String> action){
action.accept(str);
}
本例用Action代替了第一節中的自定義的delegate。其實Action就是框架幫我們預定義的泛型接口,避免我們總需自定義委托:
public static void Main(string[] args)
{
var func = (string s) => { Console.WriteLine(s); };
showLog("hello world", func);
}
public static void showLog(string str ,Action<string> action)
{
action(str);
}
總體來說,三種語言的使用方法還是比較類似的。可能是都源于C的原因?
其實對于面向對象語言來說,好多都是相通的,個人感覺經常對比一下,有助于加深記憶。
另外,如果有機會,學一門風格和自己擅長的開發語言差異比較大的,更有利于對編程語言的了解。
————————————————
版權聲明:本文為CSDN博主「FlyLolo」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Lolo_cs_dn/article/details/122159246
箭頭函數不綁定Arguments 對象。因此,在本示例中,arguments只是引用了封閉作用域內的arguments:
在大多數情況下,使用剩余參數是相較使用arguments對象的更好選擇。
如上所述,箭頭函數表達式對非方法函數是最合適的。讓我們看看當我們試著把它們作為方法時發生了什么。
箭頭函數沒有定義this綁定。另一個涉及Object.defineProperty()的示例:
箭頭函數可以有一個“簡寫體”或常見的“塊體”。
在一個簡寫體中,只需要一個表達式,并附加一個隱式的返回值。在塊體中,必須使用明確的return語句。
var func = x => x * x; // 簡寫函數 省略return var func = (x, y) => { return x + y; }; //常規編寫 明確的返回值
記住用params => {object:literal}這種簡單的語法返回對象字面量是行不通的。
var func = () => { foo: 1 }; // Calling func() returns undefined! var func = () => { foo: function() {} }; // SyntaxError: function statement requires a name
這是因為花括號({} )里面的代碼被解析為一系列語句(即 foo 被認為是一個標簽,而非對象字面量的組成部分)。
所以,記得用圓括號把對象字面量包起來:
var func = () => ({foo: 1});
箭頭函數在參數和箭頭之間不能換行。
var func = () => 1; // SyntaxError: expected expression, got '=>'
雖然箭頭函數中的箭頭不是運算符,但箭頭函數具有與常規函數不同的特殊運算符優先級解析規則。
var simple = a => a > 15 ? 15 : a; simple(16); // 15 simple(10); // 10 let max = (a, b) => a > b ? a : b;
var fact = (x) => ( x==0 ? 1 : x*fact(x-1) ); fact(5); // 120
編程是一種修行,我愿與志同道合的朋友攜手前行,一起探索有關編程的奧妙!
如果您在前端學習的過程中遇到難題,歡迎【關注】并【私信】我,大家一起交流解決!
文章推薦:
不一樣的JS函數總結,適合前端初學者的JavaScript函數代碼
JS函數聲明和函數表達式的定義及其區別——超詳講解,值得擁有
帶你一分鐘理解JS閉包——通俗易懂
JavaScript中,箭頭函數是一種簡化的函數語法,它在ES6(ECMAScript 2015)引入。箭頭函數的語法比傳統的function表達式更簡潔,同時還有一些特性,例如繼承外部作用域的this值。
箭頭函數的基本語法如下:
(param1, param2, ..., paramN) => { statements }
(param1, param2, ..., paramN) => expression
當箭頭函數只有一個參數時,可以省略括號:
param => { statements }
param => expression
當函數體只包含一個返回值表達式時,可以省略花括號并直接返回該表達式的值:
(param1, param2) => param1 + param2
需要注意的是,箭頭函數有以下特性:
更多箭頭函數的用法
箭頭函數的簡潔語法使得在鏈式調用中使用它們變得更加容易。例如,我們可以在數組上使用多個數組方法并將箭頭函數作為回調函數:
const numbers = [1, 2, 3, 4, 5];
const doubledAndFiltered = numbers
.map(num => num * 2)
.filter(num => num > 5);
console.log(doubledAndFiltered); // [6, 8, 10]
箭頭函數可以方便地用作事件處理程序,因為它們繼承了外部作用域的this值。這樣就避免了使用bind來綁定事件處理程序的需要。例如:
class Button {
constructor() {
this.buttonElement = document.createElement('button');
this.buttonElement.textContent = 'Click me!';
this.buttonElement.addEventListener('click', () => this.handleClick());
document.body.appendChild(this.buttonElement);
}
handleClick() {
console.log('Button clicked!');
}
}
const button = new Button();
常見的數組方法,如filter、reduce、forEach等,也可以與箭頭函數一起使用,以簡化代碼并使其更具可讀性:
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 15
numbers.forEach((num, index) => console.log(`Index ${index}: ${num}`));
箭頭函數還可以作為立即調用函數表達式(IIFE)使用,這在某些場景下有助于限制變量的作用域:
const result = (() => {
const localVar = 'I am only available within this IIFE';
return localVar.toUpperCase();
})();
console.log(result); // 'I AM ONLY AVAILABLE WITHIN THIS IIFE'
在箭頭函數中直接返回對象字面量時,需要注意語法。由于大括號 {} 在箭頭函數中被解釋為代碼塊,而不是對象字面量,因此需要在對象字面量周圍添加額外的括號:
const getObject = () => ({ key: 'value' });
console.log(getObject()); // { key: 'value' }
雖然箭頭函數通常用于簡潔的單行函數,但它們也可以用于多行函數。在這種情況下,需要使用大括號包裹函數體,并在需要返回值時使用 return 關鍵字:
const addWithLogging = (a, b) => {
console.log(`Adding ${a} and ${b}`);
return a + b;
};
console.log(addWithLogging(3, 4)); // 輸出 "Adding 3 and 4",然后輸出 7
箭頭函數可以與解構參數一起使用,可以更簡潔地處理對象或數組。以下是一些示例:
// 對象解構
const users = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 },
];
const getUserNames = users.map(({ name }) => name);
console.log(getUserNames); // 輸出:['Alice', 'Bob', 'Charlie']
// 數組解構
const points = [
[1, 2],
[3, 4],
[5, 6],
];
const getDistancesFromOrigin = points.map(([x, y]) => Math.sqrt(x * x + y * y));
console.log(getDistancesFromOrigin); // 輸出:[2.23606797749979, 5, 7.810249675906654]
由于箭頭函數在其詞法作用域內捕獲 this 值,因此在某些情況下可能導致問題。例如,在對象方法中使用箭頭函數時,它不會獲取到對象的 this,而是捕獲到外部作用域的 this。為了解決這個問題,需要使用傳統的 function 聲明或表達式。
const obj = {
value: 10,
getValue: function() {
// 正常的function表達式,`this` 指向obj
return this.value;
},
getValueWithArrow: () => {
// 箭頭函數,`this` 指向外部作用域(在這種情況下是全局對象或undefined)
return this.value;
},
};
console.log(obj.getValue()); // 輸出:10
console.log(obj.getValueWithArrow()); // 輸出:undefined(嚴格模式)或全局對象的value屬性
在處理高階函數時,箭頭函數非常有用,因為它們可以使代碼更簡潔。高階函數是接受一個或多個函數作為參數、返回一個函數的函數。這里有一個使用箭頭函數的高階函數示例:
const add = a => b => a + b;
const add5 = add(5);
console.log(add5(3)); // 輸出:8
在上面的示例中,add 函數接受一個參數 a 并返回一個新的函數,該函數接受另一個參數 b 并返回 a + b 的結果。
盡管箭頭函數有很多優點,但并非所有場景都適用。以下是一些避免使用箭頭函數的情況:
總之,箭頭函數的簡潔語法和特性使得它們在許多情況下都非常有用,還可以用在setTimeout、錯誤處理、Promise中等等。但是,在遇到 this、arguments 或其他相關問題時,有時可能需要使用傳統的function聲明或表達式來解決特定問題。
參考:JS中的箭頭函數 - 掘金
*請認真填寫需求信息,我們會在24小時內與您取得聯系。