I was really struggling to understand the syntax of the Backbone but failed miserably, look at the following code:
var MenuItemDetails = Backbone.Model.extend({});
I have looked at the source code of the Backbone.js and found the following declaration of Backbone.Model
var Model = Backbone.Model = function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId('c');
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
attrs = _.defaults({}, attrs, _.result(this, 'defaults'));
this.set(attrs, options);
this.changed = {};
this.initialize.apply(this, arguments);
};
as I understand var Model
is now a function, which will work as constructor. What I don't understand is:
- we are not calling
var myModelObject = new Model()
orvar myModelObject = new Backbone.Model()
. Instead we are callingBackbone.Model.extend()
. How on earth does this work as a constructor? - This call
Backbone.Model.extend()
breaks my mind. We are calling a function calledextend
on another function calledBackbone.Model