箭头函数'()=> {}'在Javascript中的含义是什么?

cno*_*gr8 15 javascript ecmascript-6 arrow-functions

我正在阅读ScrollListView的源代码,在几个地方我看到了使用() => {}.

如在线25,

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

第31行,

this.container.addEventListener('scroll', () => this.onScroll(), false);
Run Code Online (Sandbox Code Playgroud)

第88行.

resizeTimer = setTimeout(() => {
    this.containerHeight = this.container.offsetHeight;
}, 250);
Run Code Online (Sandbox Code Playgroud)

这是一个简写function,如果它有任何不同,怎么样?

thr*_*qon 23

这是ES6的新箭头语法.它的不同之处在于this:根据调用上下文(传统语义)function得到一个,但是箭头函数保留了定义的上下文.thisthis

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

  • 请注意浏览器兼容性https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions (2认同)

Jay*_*tel 6

ECMAScript 6arrow function引入了,箭头(=>)部分的arrow function语法。

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