2013-09-30 42 views
3

和有什么不同?我是CoffeeScript的新手。今天我遇到了这个。CoffeeScript,=>和 - >

example -> 
a -> 

example -> 
b => 

这是什么细箭头VS脂肪箭头之间有什么不同?

是否有人可以解释一下它们的区别以及它们应该使用的时间。

+0

这个问题似乎是题外话,因为它是关于不阅读文档。 – rlemon

+0

@ rlemon我不觉得这是脱离主题,我正在阅读文档,并不理解。 – Tyler

+0

@Tyler ==> right here:http://coffeescript.org/#fat-arrow – Neal

回答

7

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

这对回调尤其方便。

通知所产生的差异

咖啡脚本:

foo =() -> this.x + this.x; 
bar =() => this.x + this.x; 

的JavaScript

var bar, foo, 
    _this = this; 

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

bar = function() { 
    return _this.x + _this.x; 
}; 
+2

_this和this有什么区别? – Tyler

+0

@Tyler'_this'只是一个变量名。 'this'是一个语言关键字(在JS中,认为@在CS中) –

+0

ok我知道这是什么意思,我只是不清楚_this – Tyler