我的 Backbone 应用程序有一个带有静态属性的父类,以及两个子类。我正在尝试从子类修改父类的静态属性,但这似乎不起作用。这是一些示例代码:
var ParentView = Backbone.View.extend({}, {
staticProperty: 1,
getStaticProperty: function() {
return this.staticProperty;
},
setStaticProperty: function(value) {
this.staticProperty = value;
}
});
console.log('ParentView.staticProperty: ' + ParentView.getStaticProperty());
ParentView.setStaticProperty(2);
var ChildView1 = ParentView.extend({
initialize: function() {
console.log('ChildView1.staticProperty: ' + ChildView1.getStaticProperty());
ChildView1.setStaticProperty(3); // THIS SEEMS TO DO NOTHING
}
});
var ChildView2 = ParentView.extend({
initialize: function() {
console.log('ChildView2.staticProperty: ' + ChildView2.getStaticProperty());
}
});
var testView1 = new ChildView1();
var testView2 = new ChildView2();
这是一个jsfiddle:http: //jsfiddle.net/2agTW/1/
我希望得到以下输出:
ParentView.staticProperty: 1
ChildView1.staticProperty: 2
ChildView2.staticProperty: 3
但相反,我得到:
ParentView.staticProperty: 1
ChildView1.staticProperty: 2
ChildView2.staticProperty: 2 // I THINK THIS SHOULD BE 3
知道为什么吗?