ES6 笔记 箭头函数

箭头函数其实就是lambda函数,JavaScript中的匿名函数

// ES5
var selected = allJobs.filter(function (job) {
  return job.isSelected();
});

// ES6
var selected = allJobs.filter(job => job.isSelected());

多个参数时,参数外加上括号(或者使用rest 参数,参数默认值,析构参数):

var total = values.reduce((a, b) => a + b, 0);

箭头函数的执行体可以是一个block,返回值需要显式的return:

(a, b) => {
  return a+b;
}

箭头函数的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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容