19

我正在阅读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,如果它有任何不同,怎么会?

4

2 回答 2

27

这是 ES6 的新箭头语法。它的不同之处在于this:根据调用function上下文(传统语义)获取 a ,但箭头函数保留定义的上下文。thisthis

http://tc39wiki.calculist.org/es6/arrow-functions/

于 2015-03-13T11:13:29.117 回答
6

ECMAScript 6arrow function引入,Arrow(=>)部分arrow function语法。

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

于 2015-03-13T12:11:02.933 回答