【问题标题】:What does arrow function '() => {}' mean in Javascript? [duplicate]箭头函数 '() => {}' 在 Javascript 中是什么意思? [复制]
【发布时间】:2015-05-15 20:20:18
【问题描述】:

我正在阅读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 的简写吗?如果它有任何不同,那又如何?

【问题讨论】:

标签: javascript ecmascript-6 arrow-functions


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

ECMAScript 6 arrow function 引入了 arrow function 语法的箭头 (=&gt;) 部分。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 2015-09-29
    • 2017-06-01
    • 2016-12-09
    相关资源
    最近更新 更多