我是新手,正在学习CoffeeScript。今天遇到了这个问题。
example ->
a ->
and
example ->
b =>
细箭头和粗箭头之间有什么区别?
有人能解释一下它们的区别以及何时应该使用它们吗?
我是新手,正在学习CoffeeScript。今天遇到了这个问题。
example ->
a ->
and
example ->
b =>
细箭头和粗箭头之间有什么区别?
有人能解释一下它们的区别以及何时应该使用它们吗?
胖箭头符号=>
定义一个绑定到当前this
值的函数。
这对于回调非常方便。
注意生成的差异。
CoffeeScript:
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;
};
_this
只是一个变量名。this
是一种语言关键字(在JS中,类似于CS中的@)。 - Benjamin Gruenbaumthis
可能不是您的对象。 - Daniel A. White