I was just wondering some OO while reading backbone’s source.
http://backbonejs.org/docs/backbone.html (search for “this.parse(attributes);”)
Backbone.Model = function(attributes, options) {
var defaults;
attributes || (attributes = {});
console.log('attributes : ', attributes);
if (options && options.parse) {
//LOOK HERE
attributes = this.parse(attributes);
}
if (defaults = getValue(this, 'defaults')) {
attributes = _.extend({}, defaults, attributes);
}
if (options && options.collection) this.collection = options.collection;
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
if (!this.set(attributes, {silent: true})) {
throw new Error("Can't create an invalid model");
}
delete this._changed;
this._previousAttributes = _.clone(this.attributes);
this.initialize.apply(this, arguments);
};
how does it come that parse can be used there in the prototype ?
the method is defined later in _.extend(Backbone.Model.prototype, Backbone.Events, {
The code that you see in function is going to be executed when someone is doing
By that time,
Backbone.Model.prototypewill be defined as well. So property lookup will findparsemethod onthis.constructor.prototype(wherethiswill be pointing at just created instance ofBackbone.Model).