1
通過docs的閱讀,我發現可以在模型類上替換Backbone的擴展的構造函數。但是這樣做與在初始化方法上做這件事有什麼區別?當您使用new
時不會同時被呼叫?骨幹模型:初始化與構造函數
var Library = Backbone.Model.extend({
constructor: function() {
this.books = new Books();
Backbone.Model.apply(this, arguments);
},
parse: function(data, options) {
this.books.reset(data.books);
return data.library;
}
});
與
var Library = Backbone.Model.extend({
initialize: function() {
this.books = new Books();
Backbone.Model.apply(this, arguments);
},
parse: function(data, options) {
this.books.reset(data.books);
return data.library;
}
});
的[什麼是對骨幹模型初始化和構造函數之間的差別(可能的複製http://stackoverflow.com/questions/10118988/whats-the-difference-between-initialize-and -constructor-ON-A-骨架模型) –