我刚刚开始学习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,但答案似乎有所不同。
答案 0 :(得分:26)
由于你只是在询问膨胀,我们来看看一些代码。
Backbone.Model.extend
如果打开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代码进行比较。你会看到当你使用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 - 只是有一个依赖于外部库的示例是没有意义的。