我正在通過教程學習backbone.js。我嘗試了一些代碼,看起來很奇怪。我首先初始化了一個模型實例,其中src
爲thesource.jpg
,模型實例爲console.log
,然後將src
屬性設置爲aaa
,後面跟着console.log
。在backbone.js中設置模型的屬性值
在javascript控制檯中,我看到對於兩個輸出,src
是相同的aaa
。他們不應該是不同的?
JS代碼
var Photo = Backbone.Model.extend({
defaults: {
src: 'placeholder.jpg',
title: 'An image placeholder',
coordinates: [0, 0]
},
initialize: function() {
this.bind("change:src", function() {
var src = this.get("src");
console.log('Image source updated to ' + src);
});
console.log('This model has been initialized!');
},
changeSrc: function(source) {
this.set({src: source});
}
});
window.myPhoto = new Photo({title: "My awesomeness",
src: "thesource.jpg",
location: "Boston",
tags: ['big game', 'vacation']});
console.log(myPhoto.attributes);
myPhoto.set({src:'aaaa'});
console.log(myPhoto.attributes);
控制檯輸出
真的很棘手,謝謝! – Nyxynyx