TYR*_*AEL 10 javascript coffeescript backbone.js
我刚刚开始学习Coffeescript,并且无法找到我应该使用的确切答案
class Model extends Backbone.Model
urlRoot: '//some/url'
Run Code Online (Sandbox Code Playgroud)
编译成
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);
Run Code Online (Sandbox Code Playgroud)
而不是
Model = Backbone.Model.extend
urlRoot: '//some/url'
Run Code Online (Sandbox Code Playgroud)
编译成
var Model = Backbone.Model.extend({
urlRoot: '//some/url'
});
Run Code Online (Sandbox Code Playgroud)
我问的主要原因是因为前者几乎用于我所看过的所有例子.但是,与后者相比,它在编译时会产生"更多"的膨胀.我确实读过这个问题,但答案似乎有所不同.
phe*_*nal 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;
};
Run Code Online (Sandbox Code Playgroud)
当我们打电话
var Model = Backbone.Model.extend({urlRoot: '//some/url' });
Run Code Online (Sandbox Code Playgroud)
我们得到类似的东西:
// 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;
Run Code Online (Sandbox Code Playgroud)
extends将其与CoffeeScript代码进行比较.您将看到当您使用extends名为__extendsgets 的魔法函数添加到文件的开头时(格式化时)如下所示:
__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;
};
Run Code Online (Sandbox Code Playgroud)
这与生成的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);
Run Code Online (Sandbox Code Playgroud)
当我们打电话
Model extends Backbone.Model
urlRoot: '//some/url'
Run Code Online (Sandbox Code Playgroud)
我们得到类似的东西:
// 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;
Run Code Online (Sandbox Code Playgroud)
他们看起来很相似不是吗?
CoffeeScript只是JavaScript.如果您已经在使用Backbone并且想要避免__extends在生成的源中添加函数,那么请使用Backbone.Model.extend.如果你想避免在Backbone中加入所有内容,那么extends几乎可以做同样的事情.这么多例子不使用后者的原因是Backbone不需要使用CoffeeScript - 只是有一个依赖外部库的例子是没有意义的.
| 归档时间: |
|
| 查看次数: |
2940 次 |
| 最近记录: |