【问题标题】:CoffeeScript, What is the difference between => and -> [duplicate]CoffeeScript,=> 和 -> 有什么区别 [重复]
【发布时间】:2013-10-06 14:39:49
【问题描述】:

我是 CoffeeScript 新手。我今天遇到了这个。

example -> 
 a ->

example ->
 b =>

细箭头和粗箭头有什么区别?

有人可以解释一下区别以及何时应该使用它们。

【问题讨论】:

  • 这个问题似乎是题外话,因为它是关于不阅读文档。
  • @rlemon 我觉得这不是题外话,我正在阅读文档并没有理解。
  • @Tyler ==> 就在这里:coffeescript.org/#fat-arrow

标签: javascript coffeescript


【解决方案1】:

粗箭头=> 定义了一个绑定到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;
};

【讨论】:

  • _this 和 this 有什么区别?
  • @Tyler _this 只是一个变量名。 this 是语言关键字(在 JS 中,想想 CS 中的 @)
  • 好的我知道这意味着什么我只是不清楚_this
  • @Tyler - javascript 将事件和函数绑定到调用者 - 所以 this 可能不是你的对象。
猜你喜欢
  • 2017-02-17
  • 2011-09-27
  • 2013-04-22
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多