What does arrow function '() => {}' mean in Javascript? [duplicate]

时间秒杀一切 提交于 2019-11-26 06:48:30

问题


This question already has an answer here:

  • What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript? 13 answers

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

Such as on line 25,

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

line 31,

this.container.addEventListener(\'scroll\', () => this.onScroll(), false);

line 88.

resizeTimer = setTimeout(() => {
    this.containerHeight = this.container.offsetHeight;
}, 250);

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


回答1:


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.

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




回答2:


ECMAScript 6 arrow function introduce, Arrow (=>) part of the arrow function syntax.

Arrow functions work differently from traditional JavaScript functions. I'm found this article explain how is different from traditional function: http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/



来源:https://stackoverflow.com/questions/29030747/what-does-arrow-function-mean-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!