箭頭函數(shù)其實(shí)就是lambda函數(shù),JavaScript中的匿名函數(shù)
// ES5
var selected = allJobs.filter(function (job) {
return job.isSelected();
});
// ES6
var selected = allJobs.filter(job => job.isSelected());
多個參數(shù)時焙压,參數(shù)外加上括號(或者使用rest 參數(shù)扰楼,參數(shù)默認(rèn)值,析構(gòu)參數(shù)):
var total = values.reduce((a, b) => a + b, 0);
箭頭函數(shù)的執(zhí)行體可以是一個block划鸽,返回值需要顯式的return:
(a, b) => {
return a+b;
}
箭頭函數(shù)的this
從外圍作用域繼承
Arrow functions do not have their own this value. The value of this inside an arrow function is always inherited from the enclosing scope.