javascript - 将任意对象传递给 Coffeescript 的匿名函数

标签 javascript coffeescript

当我运行以下 Coffeescript 代码时:

@sum = (x, y) -> x + y

我得到了这个已编译的 Javascript:

(function() {

    this.sum = function(x, y) {
        return x + y;
    };

}).call(this);

在 Coffeescript 中有没有办法用 myObject 之类的任意对象替换 .call(this) 中的 this

最佳答案

(function() { and }).call(this); 不是编译 @sum = ... 的结果, 但由 coffee 可执行文件添加。 This是编译的实际结果:

this.sum = function(x, y) {
  return x + y;
};

要获得不同/所需的输出,请运行 coffee -b -c(或 coffee -bccoffee --bare --compile) 使用 the following code :

(-> 
  @sum = (x, y) -> x + y
).call WHATEVER

成为

(function() {
  return this.sum = function(x, y) {
    return x + y;
  };
}).call(WHATEVER);

关于javascript - 将任意对象传递给 Coffeescript 的匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106478/

相关文章:

javascript - 将 MYSQL (PHP) 值拉入 JSON -> 错误未定义

javascript - 评论展开/折叠不适用于评论 View 更多/更少

node.js - 我的 mocha 测试单独工作,但同时运行时失败

node.js - 使用node.js和lazy时了解EOF

javascript - HTML5 拖放不掉

javascript - Return 语句在 ajax 响应之前执行

javascript - 如何使用 Google Analytics 跟踪 Ajax 请求?

node.js - 在 npm install 上编译 CoffeeScript

javascript - 将 SammyJs 与 RequireJs 结合使用

javascript - 如何使用 javaScript 在 Iframe 中添加 CSS 类和控件元素?