定義:<article id="wikiArticle" style="font-style: normal !important; display: block; margin: 0px; padding: 0px 0px 20px; border-width: 0px 0px 3px; border-top-style: initial; border-right-style: initial; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-left-color: initial; border-image: initial; position: relative; border-bottom-style: solid; border-bottom-color: rgb(61, 126, 154);">
語法節(jié)
基礎(chǔ)語法節(jié)
(參數(shù)1, 參數(shù)2, …, 參數(shù)N) => { 函數(shù)聲明 } (參數(shù)1, 參數(shù)2, …, 參數(shù)N) => 表達(dá)式(單一) //相當(dāng)于:(參數(shù)1, 參數(shù)2, …, 參數(shù)N) =>{ return 表達(dá)式; } // 當(dāng)只有一個(gè)參數(shù)時(shí)跑杭,圓括號是可選的: (單一參數(shù)) => {函數(shù)聲明} 單一參數(shù) => {函數(shù)聲明} // 沒有參數(shù)的函數(shù)應(yīng)該寫成一對圓括號狮荔。 () => {函數(shù)聲明}
高級語法節(jié)
//加括號的函數(shù)體返回對象字面表達(dá)式: 參數(shù)=> ({foo: bar}) //支持剩余參數(shù)和默認(rèn)參數(shù) (參數(shù)1, 參數(shù)2, ...rest) => {函數(shù)聲明} (參數(shù)1 = 默認(rèn)值1,參數(shù)2, …, 參數(shù)N = 默認(rèn)值N) => {函數(shù)聲明} //同樣支持參數(shù)列表解構(gòu) let f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c; f(); // 6
描述節(jié)
參考 "ES6 In Depth: Arrow functions" on hacks.mozilla.org.
引入箭頭函數(shù)有兩個(gè)方面的作用:更簡短的函數(shù)并且不綁定this
幕随。
更短的函數(shù)節(jié)
var materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
materials.map(function(material) {
return material.length;
}); // [8, 6, 7, 9]
materials.map((material) => {
return material.length;
}); // [8, 6, 7, 9]
materials.map(material => material.length); // [8, 6, 7, 9]
不綁定this
節(jié)
在箭頭函數(shù)出現(xiàn)之前存捺,每個(gè)新定義的函數(shù)都有它自己的 [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)
值(在構(gòu)造函數(shù)的情況下是一個(gè)新對象,在嚴(yán)格模式的函數(shù)調(diào)用中為 undefined行您,如果該函數(shù)被作為“對象方法”調(diào)用則為基礎(chǔ)對象等)。This
被證明是令人厭煩的面向?qū)ο箫L(fēng)格的編程。
function Person() {
// Person() 構(gòu)造函數(shù)定義 `this`作為它自己的實(shí)例.
this.age = 0;
setInterval(function growUp() {
// 在非嚴(yán)格模式, growUp()函數(shù)定義 `this`作為全局對象,
// 與在 Person()構(gòu)造函數(shù)中定義的 `this`并不相同.
this.age++;
}, 1000);
}
var p = new Person();
在ECMAScript 3/5中缭受,通過將this
值分配給封閉的變量,可以解決this
問題该互。
function Person() {
var that = this;
that.age = 0;
setInterval(function growUp() {
// 回調(diào)引用的是`that`變量, 其值是預(yù)期的對象.
that.age++;
}, 1000);
}
或者米者,可以創(chuàng)建綁定函數(shù),以便將預(yù)先分配的this
值傳遞到綁定的目標(biāo)函數(shù)(上述示例中的growUp()
函數(shù))宇智。
箭頭函數(shù)不會創(chuàng)建自己的this,它只會從自己的作用域鏈的上一層繼承this
蔓搞。因此,在下面的代碼中随橘,傳遞給setInterval
的函數(shù)內(nèi)的this
與封閉函數(shù)中的this
值相同:
function Person(){
this.age = 0;
setInterval(() => {
this.age++; // |this| 正確地指向person 對象
}, 1000);
}
var p = new Person();
與嚴(yán)格模式的關(guān)系
鑒于 this
是詞法層面上的喂分,嚴(yán)格模式中與 this
相關(guān)的規(guī)則都將被忽略。
function Person() {
this.age = 0;
var closure = "123"
setInterval(function growUp() {
this.age++;
console.log(closure)
}, 1000);
}
var p = new Person();
function PersonX() {
'use strict'
this.age = 0;
var closure = "123"
setInterval(()=>{
this.age++;
console.log(closure)
}, 1000);
}
var px = new PersonX();
嚴(yán)格模式的其他規(guī)則依然不變.
通過 call 或 apply 調(diào)用
由于 箭頭函數(shù)沒有自己的this指針机蔗,通過 call()
* 或* apply()
方法調(diào)用一個(gè)函數(shù)時(shí)蒲祈,只能傳遞參數(shù)(不能綁定this---譯者注),他們的第一個(gè)參數(shù)會被忽略萝嘁。(這種現(xiàn)象對于bind方法同樣成立---譯者注)
var adder = {
base : 1,
add : function(a) {
var f = v => v + this.base;
return f(a);
},
addThruCall: function(a) {
var f = v => v + this.base;
var b = {
base : 2
};
return f.call(b, a);
}
};
console.log(adder.add(1)); // 輸出 2
console.log(adder.addThruCall(1)); // 仍然輸出 2(而不是3 ——譯者注)
不綁定arguments
節(jié)
箭頭函數(shù)不綁定Arguments 對象梆掸。因此,在本示例中牙言,arguments
只是引用了封閉作用域內(nèi)的arguments:
var arguments = [1, 2, 3];
var arr = () => arguments[0];
arr(); // 1
function foo(n) {
var f = () => arguments[0] + n; // 隱式綁定 foo 函數(shù)的 arguments 對象. arguments[0] 是 n
return f();
}
foo(1); // 2
在大多數(shù)情況下酸钦,使用剩余參數(shù)是相較使用arguments
對象的更好選擇。
function foo() {
var f = (...args) => args[0];
return f(2);
}
foo(1);
// 2
像函數(shù)一樣使用箭頭函數(shù)節(jié)
如上所述咱枉,箭頭函數(shù)表達(dá)式對非方法函數(shù)是最合適的卑硫。讓我們看看當(dāng)我們試著把它們作為方法時(shí)發(fā)生了什么徒恋。
'use strict';
var obj = {
i: 10,
b: () => console.log(this.i, this),
c: function() {
console.log( this.i, this)
}
}
obj.b();
// undefined
obj.c();
// 10, Object {...}
箭頭函數(shù)沒有定義this綁定。另一個(gè)涉及Object.defineProperty()
的示例:
'use strict';
var obj = {
a: 10
};
Object.defineProperty(obj, "b", {
get: () => {
console.log(this.a, typeof this.a, this);
return this.a+10;
// 代表全局對象 'Window', 因此 'this.a' 返回 'undefined'
}
});
使用 new
操作符節(jié)
箭頭函數(shù)不能用作構(gòu)造器拔恰,和 new
一起用會拋出錯(cuò)誤因谎。
var Foo = () => {};
var foo = new Foo(); // TypeError: Foo is not a constructor
使用prototype
屬性節(jié)
箭頭函數(shù)沒有prototype
屬性。
var Foo = () => {};
console.log(Foo.prototype); // undefined
使用 yield
關(guān)鍵字節(jié)
[yield](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield)
關(guān)鍵字通常不能在箭頭函數(shù)中使用(除非是嵌套在允許使用的函數(shù)內(nèi))颜懊。因此财岔,箭頭函數(shù)不能用作生成器。
函數(shù)體節(jié)
箭頭函數(shù)可以有一個(gè)“簡寫體”或常見的“塊體”河爹。
在一個(gè)簡寫體中匠璧,只需要一個(gè)表達(dá)式,并附加一個(gè)隱式的返回值咸这。在塊體中夷恍,必須使用明確的return
語句。
var func = x => x * x;
// 簡寫函數(shù) 省略return
var func = (x, y) => { return x + y; };
//常規(guī)編寫 明確的返回值
返回對象字面量節(jié)
記住用params => {object:literal}
這種簡單的語法返回對象字面量是行不通的媳维。
var func = () => { foo: 1 };
// Calling func() returns undefined!
var func = () => { foo: function() {} };
// SyntaxError: function statement requires a name
這是因?yàn)榛ɡㄌ枺?code>{} )里面的代碼被解析為一系列語句(即 foo
被認(rèn)為是一個(gè)標(biāo)簽酿雪,而非對象字面量的組成部分)。
所以侄刽,記得用圓括號把對象字面量包起來:
var func = () => ({foo: 1});
換行節(jié)
箭頭函數(shù)在參數(shù)和箭頭之間不能換行指黎。
var func = ()
=> 1;
// SyntaxError: expected expression, got '=>'
解析順序節(jié)
雖然箭頭函數(shù)中的箭頭不是運(yùn)算符,但箭頭函數(shù)具有與常規(guī)函數(shù)不同的特殊運(yùn)算符優(yōu)先級解析規(guī)則州丹。
let callback;
callback = callback || function() {}; // ok
callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments
callback = callback || (() => {}); // ok
更多示例節(jié)
// 空的箭頭函數(shù)返回 undefined
let empty = () => {};
(() => 'foobar')();
// Returns "foobar"
// (這是一個(gè)立即執(zhí)行函數(shù)表達(dá)式,可參閱 'IIFE'術(shù)語表)
var simple = a => a > 15 ? 15 : a;
simple(16); // 15
simple(10); // 10
let max = (a, b) => a > b ? a : b;
// Easy array filtering, mapping, ...
var arr = [5, 6, 13, 0, 1, 18, 23];
var sum = arr.reduce((a, b) => a + b);
// 66
var even = arr.filter(v => v % 2 == 0);
// [6, 0, 18]
var double = arr.map(v => v * 2);
// [10, 12, 26, 0, 2, 36, 46]
// 更簡明的promise鏈
promise.then(a => {
// ...
}).then(b => {
// ...
});
// 無參數(shù)箭頭函數(shù)在視覺上容易分析
setTimeout( () => {
console.log('I happen sooner');
setTimeout( () => {
// deeper code
console.log('I happen later');
}, 1);
}, 1);
箭頭函數(shù)也可以使用條件(三元)運(yùn)算符:
var simple = a => a > 15 ? 15 : a;
simple(16); // 15
simple(10); // 10
let max = (a, b) => a > b ? a : b;
箭頭函數(shù)內(nèi)定義的變量及其作用域
// 常規(guī)寫法
var greeting = () => {let now = new Date(); return ("Good" + ((now.getHours() > 17) ? " evening." : " day."));}
greeting(); //"Good day."
console.log(now); // ReferenceError: now is not defined 標(biāo)準(zhǔn)的let作用域
// 參數(shù)括號內(nèi)定義的變量是局部變量(默認(rèn)參數(shù))
var greeting = (now=new Date()) => "Good" + (now.getHours() > 17 ? " evening." : " day.");
greeting(); //"Good day."
console.log(now); // ReferenceError: now is not defined
// 對比:函數(shù)體內(nèi){}不使用var定義的變量是全局變量
var greeting = () => {now = new Date(); return ("Good" + ((now.getHours() > 17) ? " evening." : " day."));}
greeting(); //"Good day."
console.log(now); // Fri Dec 22 2017 10:01:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
// 對比:函數(shù)體內(nèi){} 用var定義的變量是局部變量
var greeting = () => {var now = new Date(); return ("Good" + ((now.getHours() > 17) ? " evening." : " day."));}
greeting(); //"Good day."
console.log(now); // ReferenceError: now is not defined
箭頭函數(shù)也可以使用閉包:
// 標(biāo)準(zhǔn)的閉包函數(shù)
function A(){
var i=0;
return function b(){
return (++i);
};
};
var v=A();
v(); //1
v(); //2
//箭頭函數(shù)體的閉包( i=0 是默認(rèn)參數(shù))
var Add = (i=0) => {return (() => (++i) )};
var v = Add();
v(); //1
v(); //2
//因?yàn)閮H有一個(gè)返回醋安,return 及括號()也可以省略
var Add = (i=0)=> ()=> (++i);
箭頭函數(shù)遞歸
var fact = (x) => ( x==0 ? 1 : x*fact(x-1) );
fact(5); // 120
規(guī)范
| Specification | Status | Comment |
| ECMAScript 2015 (6th Edition, ECMA-262)
<small lang="zh-CN" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px;">Arrow Function Definitions</small> | Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262)
<small lang="zh-CN" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px;">Arrow Function Definitions</small> | Draft | |
瀏覽器兼容節(jié)
Update compatibility data on GitHub
<abbr class="only-icon" title="Desktop" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Desktop</abbr> | <abbr class="only-icon" title="Mobile" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Mobile</abbr> | <abbr class="only-icon" title="Server" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Server</abbr> | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<abbr class="only-icon" title="Chrome" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Chrome</abbr> | <abbr class="only-icon" title="Edge" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Edge</abbr> | <abbr class="only-icon" title="Firefox" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Firefox</abbr> | <abbr class="only-icon" title="Internet Explorer" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Internet Explorer</abbr> | <abbr class="only-icon" title="Opera" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Opera</abbr> | <abbr class="only-icon" title="Safari" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Safari</abbr> | <abbr class="only-icon" title="Android webview" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Android webview</abbr> | <abbr class="only-icon" title="Chrome for Android" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Chrome for Android</abbr> | <abbr class="only-icon" title="Edge Mobile" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Edge Mobile</abbr> | <abbr class="only-icon" title="Firefox for Android" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Firefox for Android</abbr> | <abbr class="only-icon" title="Opera for Android" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Opera for Android</abbr> | <abbr class="only-icon" title="iOS Safari" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">iOS Safari</abbr> | <abbr class="only-icon" title="Samsung Internet" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Samsung Internet</abbr> | <abbr class="only-icon" title="Node.js" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Node.js</abbr> | |
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
Basic support | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>45 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>Yes | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>22 |
<abbr class="only-icon" title="See implementation notes" style="font-style: normal !important; padding: 0px; border: 0px; cursor: help; text-decoration: none; margin: 0px 2px;">Notes</abbr>
打開 | <abbr class="bc-level-no only-icon" title="No support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">No support</abbr>No | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>32 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>10 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>45 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>45 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>Yes | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>22
<abbr class="only-icon" title="See implementation notes" style="font-style: normal !important; padding: 0px; border: 0px; cursor: help; text-decoration: none; margin: 0px 2px;">Notes</abbr>
打開 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>32 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>10 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>5.0 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>Yes |
| Trailing comma in parameters | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>58 | <abbr title="Compatibility unknown; please update this." style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">?</abbr> | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>52 | <abbr class="bc-level-no only-icon" title="No support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">No support</abbr>No | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>45 | <abbr title="Compatibility unknown; please update this." style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">?</abbr> | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>58 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>58 | <abbr title="Compatibility unknown; please update this." style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">?</abbr> | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>52 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>45 | <abbr title="Compatibility unknown; please update this." style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">?</abbr> | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>7.0 | <abbr class="bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: none;">Full support</abbr>Yes |
Legend
<dl style="font-style: normal !important; margin: 0px 0px 20px; padding: 0px; border: 0px; box-sizing: border-box; max-width: 42rem; display: grid; grid-template-columns: 30px 1fr 30px 1fr;">
<dt style="padding: 0px; border: 0px; font-style: normal; font-weight: 700; display: block; margin: 0px 0px 5px;"><abbr class="bc-level bc-level-yes only-icon" title="Full support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: underline dotted;">Full support </abbr></dt>
<dd style="font-style: normal !important; padding: 0px 0px 0px 20px; border: 0px; display: block; margin: 0px 10px 5px;">Full support</dd>
<dt style="padding: 0px; border: 0px; font-style: normal; font-weight: 700; display: block; margin: 0px 0px 5px;"><abbr class="bc-level bc-level-no only-icon" title="No support" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: underline dotted;">No support </abbr></dt>
<dd style="font-style: normal !important; padding: 0px 0px 0px 20px; border: 0px; display: block; margin: 0px 10px 5px;">No support</dd>
<dt style="padding: 0px; border: 0px; font-style: normal; font-weight: 700; display: block; margin: 0px 0px 5px;"><abbr class="bc-level bc-level-unknown only-icon" title="Compatibility unknown" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: underline dotted;">Compatibility unknown </abbr></dt>
<dd style="font-style: normal !important; padding: 0px 0px 0px 20px; border: 0px; display: block; margin: 0px 10px 5px;">Compatibility unknown</dd>
<dt style="padding: 0px; border: 0px; font-style: normal; font-weight: 700; display: block; margin: 0px 0px 5px;"><abbr class="only-icon" title="See implementation notes." style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px; cursor: help; text-decoration: underline dotted;">See implementation notes.</abbr></dt>
<dd style="font-style: normal !important; padding: 0px 0px 0px 20px; border: 0px; display: block; margin: 0px 10px 5px;">See implementation notes.</dd>
</dl>
相關(guān)鏈接節(jié)
</article>
文檔標(biāo)簽和貢獻(xiàn)者
標(biāo)簽:
此頁面的貢獻(xiàn)者: wangbangkun, ColinJinag, zjjgsc, Harleywww, huangll, LeoQuote, jjc, ywjco, Warden, xgqfrms-GitHub, zhangchen, anjia, StevenYuysy,ZZES_REN, tjyas, Gary-c, linzhihuan, guonanci, shifengchen, unliar, MichelleGuan, slimeball, LangDonHJJ, zhangzju, Aisi, muzhen, Meteormatt, Ende93, Ovilia,solome, zilong-thu, jy1989, teoli, ziyunfei
最后編輯者: wangbangkun, <time datetime="2018-07-30T01:34:50.215940-07:00" style="font-style: normal !important; margin: 0px; padding: 0px; border: 0px;">Jul 30, 2018, 1:34:50 AM</time>
<nav class="crumbs" role="navigation" style="font-style: normal !important; display: block; border-style: solid; border-color: rgb(215, 215, 215); border-image: initial; border-width: 0px 0px 1px; margin: 0px 0px 20px; padding: 0px 0px 15px; font-size: 0.88889rem;">
- Web 技術(shù)文檔
- Java<wbr style="font-style: normal !important;">Script
- Java<wbr style="font-style: normal !important;">Script 參考文檔
- 函數(shù)
- 箭頭函數(shù)
</nav>
箭頭函數(shù)傳參之用變量解構(gòu)的方式來傳參
let set = ({num1,num2}) => {
return num1 + num2;
}
console.log(set({num1:3,num2:4}))
普通方式傳參
let set = (num1,num2) => {
return num1 + num2;
}
console.log(set(3,4))
`//箭頭函數(shù)簡單版`
// 1、當(dāng)函數(shù)參數(shù)只有一個(gè)的時(shí)候墓毒,()可以省略吓揪,但是沒有參數(shù)的時(shí)候,括號不可以省略
//2所计、當(dāng)函數(shù)體{}中只有一行return語句的時(shí)候柠辞,中括號以及return語句可以省略。
//以下是個(gè)例子
let add = a => a;
console.log(add(3));