某人可以在Backbone中向我解釋範圍嗎?在下面的例子中,使用get方法檢索默認變量的值是最好的方法嗎?Backbone Scope訪問變量的最佳做法
如果我不使用get方法,我的數組就會以undefined的形式出現。
App.Models.Site = Backbone.Model.extend({
defaults: {
testArray: [1, 2, 3]
},
initialize: function(){
console.log('initialize', this.testArray); // logs undefined
console.log('initialize', this.get('testArray')); // logs [1, 2, 3]
this.test();
},
test: function(){
console.log('initialize', this.testArray); // logs undefined
console.log('test', this.get('testArray')); // logs [1, 2, 3]
}
});
您是否希望爲模型實例獲取'testArray'的默認值或'testArray'的實際值?我的文章是否回答你的問題?儘管我最初誤解了你正在尋找的東西並編輯了我的答案,但現在我認爲它可能是正確的。 – 2013-02-18 12:02:24