我遇到了一個我無法解釋的場景/錯誤,因爲我有一個具有數組變量的Backbone View類,但是即使在我重新實例化它之後該變量值仍然存在。骨幹觀點有以下設置:BackboneJS - 查看重新實例化後保留的類數組變量
var TestView = Backbone.View.extend({
a:"",
b:"",
items:[],
initialize:function(){
},
add:function(value){
this.items.push(value);
}
});
這是我如何實例化類:
this.formView = new TestView();
this.formView.add('halo');
this.formView.a = 'abc';
this.formView = new TestView();
this.formView.add('test');
this.formView.b = 'bcd';
console.log("a - " + this.formView.a);
console.log("b - " + this.formView.b);
console.log("items - ");
console.log(this.formView.items);
結果:
a -
b - bcd
items - ["halo", "test"]
出人意料的是,數組變量 '項目' 繼續存在,它所示的兩種['halo','test']在一起。但不適用於正常變量。
這裏是JsFiddle link。
它可以通過在初始化函數中清除數組來解決。
initialize:function(){
this.items = [];
},
但我想知道這是一個錯誤還是我誤解了一些東西。
我認爲由於某種原因,在重新實例化期間formView沒有實例化畢竟 – Deeptechtons