-4
我正在学习一些Backbone,我很困惑this
在模型中。Backbone模型中的`this`是什么?
Person = Backbone.Model.extend({
initialize: function() {
console.log('hello world');
this.bind("change:name", function() {
console.log(this.get('name') + " is now the value for name");
});
this.bind('invalid', function(model, error) {
console.error(error);
});
},
defaults: {
name: "Bob Hope",
height: "unknown"
},
validate: function (attributes) {
if(attributes.name == 'Joe') {
return "Uh oh, you're name is Joe!";
}
}
});
var person = new Person();
person.set({name: "Joe", height:"6 feet"}, {validate:true});
console.log(person.toJSON());
this.bind
是怎么回事? change:name
是什么? initialize
和defaults
只是JavaScript对象内的方法吗?
你可以找到所有的答案[backbonejs网站(http://backbonejs.org/#Events-catalog)。另请参阅[注释源代码](http://backbonejs.org/docs/backbone.html#section-51) –