Coffeescript类比Backbone扩展更加膨胀

时间:2013-08-13 06:36:05

标签: javascript backbone.js coffeescript

我刚刚开始学习Coffeescript而无法找到我应该使用的明确答案

class Model extends Backbone.Model
    urlRoot: '//some/url'

编译到

Model = (function(_super) {
    __extends(Model, _super);

    function Model() {
        _ref = Model.__super__.constructor.apply(this, arguments);
        return _ref;
    }

    Model.prototype.urlRoot = '//some/url';

    return Model;

})(Backbone.Model);

而不是

Model = Backbone.Model.extend
    urlRoot: '//some/url'

编译到

var Model = Backbone.Model.extend({
    urlRoot: '//some/url'
});

我要问的主要原因是因为前者几乎用于我所看过的所有例子中。但是,与后者相比,它在编译时会产生“更多”的膨胀。我确实读过这个question,但答案似乎有所不同。

1 个答案:

答案 0 :(得分:26)

由于你只是在询问膨胀,我们来看看一些代码。

Backbone.Model.extend

的JavaScript

如果打开Backbone源代码,您会看到extend函数如下:

var extend = function(protoProps, staticProps) {
    var parent = this;
    var child;

    if (protoProps && _.has(protoProps, 'constructor')) { // _.has comes from
      child = protoProps.constructor;                     // underscore, even 
    } else {                                              // more 'bloat'
      child = function(){ return parent.apply(this, arguments); };
    }

    _.extend(child, parent, staticProps);                // more underscore

    var Surrogate = function(){ this.constructor = child; };
    Surrogate.prototype = parent.prototype;
    child.prototype = new Surrogate;

    if (protoProps) _.extend(child.prototype, protoProps);

    child.__super__ = parent.prototype;

    return child;
  };

这里实际发生了什么:

当我们打电话时

var Model = Backbone.Model.extend({urlRoot: '//some/url' });

我们得到类似的东西:

  // Create new constructor which calls the parent constructor
  var Model;
  if (({}).hasOwnProperty.call({urlRoot: '//some/url' }, 'constructor') {
      // this is false so...                    
  } else {
      Model = function(){ return Backbone.Model.apply(this, arguments); };
  }

  // Set up prototype chain
  var Surrogate = function(){ this.constructor = model; };
  Surrogate.prototype = Backbone.Model.prototype;
  Model.prototype = new Surrogate;

  // Add properties to the child prototype
  // Same as:
  // Model.prototype.urlRoot = '//some/url';
  _.extend(Model.prototype, { urlRoot: '//some/url' });

  // Set the magical __super__ property
  Model.__super__ = Backbone.Model.prototype;

带有extends

的CoffeeScript

将其与CoffeeScript代码进行比较。你会看到当你使用extends时,一个名为__extends的神奇函数被添加到文件的开头,(格式化时)看起来像:

__extends = function(child, parent) { 
    for (var key in parent) { 
        if (__hasProp.call(parent, key)) 
            child[key] = parent[key]; 
    }

    function ctor() { this.constructor = child; } 
    ctor.prototype = parent.prototype; 
    child.prototype = new ctor(); 

    child.__super__ = parent.prototype; 

    return child; 
};

与生成的JS结合使用:

var Model = (function(_super) {
    __extends(Model, _super);

    function Model() {
        _ref = Model.__super__.constructor.apply(this, arguments);
        return _ref;
    }

    Model.prototype.urlRoot = '//some/url';

    return Model;

})(Backbone.Model);

这里实际发生了什么:

当我们打电话时

Model extends Backbone.Model
    urlRoot: '//some/url'

我们得到类似的东西:

// Create new constructor which calls the parent constructor
var Model = function () {
    return Model.__super__.constructor.apply(this, arguments);
}

// Copy static properties from Backbone.Model to Model
for (var key in Backbone.Model) {
    if (__hasProp.call(Backbone.Model, key)) 
        Model[key] = Backbone.Model[key]; 
}

// Set up prototype chain
function ctor() { this.constructor = Model; } 
ctor.prototype = Backbone.Model.prototype; 
Model.prototype = new ctor(); 

// Add properties to the child prototype
Model.prototype.urlRoot = '//some/url';

// Set the magical __super__ property
Model.__super__ = Backbone.Model.prototype; 

我们看到了什么?

它们看起来非常相似吗?

CoffeeScript只是JavaScript。如果您已经在使用Backbone,并且希望避免在生成的源中添加__extends函数,请使用Backbone.Model.extend。如果你想避免一起加入Backbone,那么extends几乎可以做同样的事情。这么多示例不使用后者的原因是Backbone不需要使用CoffeeScript - 只是有一个依赖于外部库的示例是没有意义的。