CoffeeScript,=>和 - >之间有什么区别

Tyl*_*ler 4 javascript coffeescript

我是CoffeeScript的新手.我今天碰到了这个.

example -> 
 a ->
Run Code Online (Sandbox Code Playgroud)

example ->
 b =>
Run Code Online (Sandbox Code Playgroud)

瘦箭和胖箭的区别是什么?

有人可以解释这些差异以及何时应该使用它们.

Dan*_*ite 11

胖箭头=>定义了一个绑定到当前值的函数this.

这对于回调尤其方便.

注意生成的差异

咖啡脚本:

foo = () -> this.x + this.x;
bar = () => this.x + this.x;
Run Code Online (Sandbox Code Playgroud)

JavaScript的

var bar, foo,
  _this = this;

foo = function() {
  return this.x + this.x;
};

bar = function() {
  return _this.x + _this.x;
};
Run Code Online (Sandbox Code Playgroud)

  • _this和this有什么区别? (2认同)