你说的扩展是什么意思?
- 你想合并你的对象吗?
- 你想添加自定义“方法”
SomeModel
吗?
- 您是否希望创建一个同时具有 和 的方法和属性的新
SomeModel
对象SomeObject
?
JSFiddle
var SomeObject = function() {};
SomeObject.CLASS_ATTRIBUTE = 'something';
SomeObject.prototype.aFunction = function() { return Math.E; };
SomeObject.prototype.attrB = 'wat';
var SomeModel = Backbone.Model.extend({});
// Merging instances of objects
var newSomeObject = new SomeObject(),
newSomeModel = new SomeModel(),
mergedObjects = $.extend(true, newSomeModel, newSomeModel);
console.log(mergedObjects);
// Prototyping
var mergedProto = $.extend(true, SomeModel.prototype, SomeObject.prototype);
console.log(mergedProto);
//factory
var myMergefactory = function() {
var ret = $.extend(true, SomeModel.prototype, SomeObject.prototype);
return ret;
}
作为说明,这也是有效的......
var SomeModel = Backbone.Model.extend({});
SomeModel.CLASS_ATTRIBUTE = 'something';
SomeModel.prototype.aFunction = function() { return Math.E; };
SomeModel.prototype.attrB = 'wat';
或这个...
var SomeModel = Backbone.Model.extend({
initialize: function (params) {
$.extend(true, this, params.obj);
}
});
var mObj = new SomeModel({obj: new SomeObject()});