Javascript 是否为主干。*.extend为定义的类设置protoypal值?
在将extend与集合和模型一起使用时,是不是将主干方法设置为定义的“类”的原型值的属性?这意味着,如果这些属性在类的一个实例中被修改,那么它们在类的所有实例中都会被更改 我遇到了一个与下面类似的问题 例如:Javascript 是否为主干。*.extend为定义的类设置protoypal值?,javascript,backbone.js,prototype,Javascript,Backbone.js,Prototype,在将extend与集合和模型一起使用时,是不是将主干方法设置为定义的“类”的原型值的属性?这意味着,如果这些属性在类的一个实例中被修改,那么它们在类的所有实例中都会被更改 我遇到了一个与下面类似的问题 例如: var MyCollection = Backbone.Collection.extend({ options: { someAttribute: null, anotherAttribute : null }, url : function() { return
var MyCollection = Backbone.Collection.extend({
options:
{
someAttribute: null,
anotherAttribute : null
},
url : function()
{
return this.options.someAttribute + "/" + this.options.anotherAttribute
});
var myCollectionInstance1 = new MyCollection();
_.extend(myCollectionInstance1.options,{
someAttribute: "page1",
anotherAttribute : "location1"});
var myCollectionInstance2 = new MyCollection();
_.extend(myCollectionInstance2.options,{
someAttribute: "page1",
anotherAttribute : "location2"});
// I call fetch here which will eventually run into my redefined url
// and now my url for this function is going to be
// "page1/location2" instead of what I expect "page1/location1"
// I assume this is because the protoype is being changed in the above
// of myCollectionInstance2.options
myCollectionInstance1.fetch();
如果是这种情况,那么将实例变量附加到集合的最佳方法是什么?您的问题在于执行此操作时:
var MyCollection = Backbone.Collection.extend({
options:
{
someAttribute: null,
anotherAttribute : null
}
_.extend(myCollectionInstance1.options,{ ...
您正在创建一个与所有实例共享的对象{someAttribute:null,另一个属性:null}
,执行此操作时:
var MyCollection = Backbone.Collection.extend({
options:
{
someAttribute: null,
anotherAttribute : null
}
_.extend(myCollectionInstance1.options,{ ...
您正在更新此共享对象
问题的解决方案是当您要设置集合时。选项
创建新对象:
myCollectionInstance1.options = {
someAttribute: "page1",
anotherAttribute : "location1"
};
...
myCollectionInstance2.options = {
someAttribute: "page1",
anotherAttribute : "location2"
};
这样,每个集合将保存自己的对象。您的问题在于执行此操作时:
var MyCollection = Backbone.Collection.extend({
options:
{
someAttribute: null,
anotherAttribute : null
}
_.extend(myCollectionInstance1.options,{ ...
您正在创建一个与所有实例共享的对象{someAttribute:null,另一个属性:null}
,执行此操作时:
var MyCollection = Backbone.Collection.extend({
options:
{
someAttribute: null,
anotherAttribute : null
}
_.extend(myCollectionInstance1.options,{ ...
您正在更新此共享对象
问题的解决方案是当您要设置集合时。选项
创建新对象:
myCollectionInstance1.options = {
someAttribute: "page1",
anotherAttribute : "location1"
};
...
myCollectionInstance2.options = {
someAttribute: "page1",
anotherAttribute : "location2"
};
这样,每个集合都将保存自己的对象。是的,
extend
的第一个参数中的所有内容都将在原型中结束,因此由实例共享。可变属性(如对象和数组)的常见解决方案是在初始化中指定它们:
var MyCollection = Backbone.Collection.extend({
initialize: function(options) {
this.options = {
someAttribute: null,
anotherAttribute: null
};
// The rest of the initialization goes here...
}
});
var MyCollection = Backbone.Collection.extend({
default_options: {
someAttribute: null,
anotherAttribute: null
},
initialize: function(options) {
this.options = _(this.default_options).clone();
// The rest of the initialization goes here...
}
});
如果您想将它们保存在文档中,则可以在中初始化:
var MyCollection = Backbone.Collection.extend({
initialize: function(options) {
this.options = {
someAttribute: null,
anotherAttribute: null
};
// The rest of the initialization goes here...
}
});
var MyCollection = Backbone.Collection.extend({
default_options: {
someAttribute: null,
anotherAttribute: null
},
initialize: function(options) {
this.options = _(this.default_options).clone();
// The rest of the initialization goes here...
}
});
请注意,.clone
只进行浅层复制,因此如果此.options
包含嵌入的数组或对象,您仍然可以意外共享。是的,extend
的第一个参数中的所有内容都会在原型中结束,从而由实例共享。可变属性(如对象和数组)的常见解决方案是在初始化中指定它们:
var MyCollection = Backbone.Collection.extend({
initialize: function(options) {
this.options = {
someAttribute: null,
anotherAttribute: null
};
// The rest of the initialization goes here...
}
});
var MyCollection = Backbone.Collection.extend({
default_options: {
someAttribute: null,
anotherAttribute: null
},
initialize: function(options) {
this.options = _(this.default_options).clone();
// The rest of the initialization goes here...
}
});
如果您想将它们保存在文档中,则可以在中初始化:
var MyCollection = Backbone.Collection.extend({
initialize: function(options) {
this.options = {
someAttribute: null,
anotherAttribute: null
};
// The rest of the initialization goes here...
}
});
var MyCollection = Backbone.Collection.extend({
default_options: {
someAttribute: null,
anotherAttribute: null
},
initialize: function(options) {
this.options = _(this.default_options).clone();
// The rest of the initialization goes here...
}
});
请注意,\uu.clone
只进行浅层复制,因此如果此.options
包含嵌入的数组或对象,您仍然可以意外共享