箭头函数'()=> {}“是指在Javascript? [英] What does arrow function '() => {}' mean in Javascript?

查看:105
本文介绍了箭头函数'()=> {}“是指在Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 ScrollListView 的来源,并在几个地方我看到使用()=> {}

I was reading the source for ScrollListView and in several places I see the use of () => {}.

如第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);

这是函数的缩写,如果它有什么不同,怎么样?

Is this a shorthand for function and if it differs in any way, how so?

推荐答案

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

This is the new arrow syntax of ES6. It differs by the treatment of this: function gets a this according to the calling context (traditional semantics), but the arrow functions keep the this of the context of definition.

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

这篇关于箭头函数'()=&gt; {}“是指在Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆