以下赋值在coffeescript片段中的含义是什么?

时间:2015-07-05 13:25:36

标签: coffeescript

我在类定义

之后看到了一个A类
class A
   constructor: (@x=0, @y=0) ->

@A = A

@A = A在这里是什么?

1 个答案:

答案 0 :(得分:2)

查看生成的输出会有所帮助。

// Generated by CoffeeScript 1.9.3
(function() {
  var A;

  A = (function() {
    function A(x, y) {
      this.x = x != null ? x : 0;
      this.y = y != null ? y : 0;
    }

    return A;

  })();

  this.A = A;

}).call(this);

因此@A会转换为this.A。在顶级使用this时,它会引用window。所以@A = A将A类导出到窗口对象上。

这通常用于导出库。例如,window.$ = jQuerywindow._ = lodash

相关问题