某人可以在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