什么是箭头功能'()=> {}'在Javascript中意味着什么?

时间:2015-03-13 11:11:42

标签: javascript ecmascript-6 arrow-functions

我正在阅读ScrollListView的来源,在一些地方,我看到() => {}的使用。

如第25行,

this.cellReorderThreshold = () => {
    var ratio = (this.CELLHEIGHT*this.cellsWithinViewportCount)/4;
    return ratio < this.CELLHEIGHT ? 0 : ratio;
};

第31行,

this.container.addEventListener('scroll', () => this.onScroll(), false);

第88行。

resizeTimer = setTimeout(() => {
    this.containerHeight = this.container.offsetHeight;
}, 250);

这是function的简写,如果它有什么不同,怎么会这样?

2 个答案:

答案 0 :(得分:22)

这是ES6的新箭头语法。它与[{1}}的处理方式不同:this根据调用上下文(传统语义)得到function,但箭头函数保留{{1 定义的上下文。

请参阅http://tc39wiki.calculist.org/es6/arrow-functions/

答案 1 :(得分:4)

  

ECMAScript 6 arrow function 介绍 (=>) 语法中的箭头 arrow function 部分。

箭头函数与传统JavaScript函数的工作方式不同。我发现这篇文章解释了与传统功能的不同之处:http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/

相关问题