javascript - 箭头函数 '() => {}' 在 Javascript 中是什么意思?

标签 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 的简写吗?如果有任何不同,那是怎么回事?

最佳答案

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

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

关于javascript - 箭头函数 '() => {}' 在 Javascript 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29030747/

相关文章:

node.js - AVA 测试设置 : "Unexpected token export"

javascript - 当 `this`没有引用对象时如何调用对象方法?

memory-management - 在 CoffeeScript 中构建类时,是否有理由不使用实例方法的粗箭头?

javascript - js如何从数组中取出最小的两个数?

javascript - 从 Javascript 访问 Chrome 的内部 gzip 例程

javascript - 在javascript中将DateTimeOffset字符串值转换为Date对象

javascript - 如何访问另一个数组中的对象数组中的 ID 或名称?

javascript - Skrollr 锚定目标问题

javascript - Object.assign 和代理

javascript - 获取 jquery 和 ecmascript6 中的最高属性编号