Looking through Backbones source I noticed that classes are defined like this:
var Model = Backbone.Model = function(...
Why is Model defined twice I ask myself, a search for Model showed that the prototype of Model is then extended like so:
_.extend(Model.prototype, Events, {
I did a little test and it seems that if ‘Model.prototype’ is being extended the changes automatically affect Backbone.Model, what’s the reason behind this and why wasn’t it instead written written like this:
Backbone.Model = function(...
Backbone.Model.prototype.extend(...
Hopefully this will lift my understanding of prototypal inheritance up a notch
It’s just a shortcut to have a local variable named
Modelwhich is shorter to type thanBackbone.Model. Javascript doesn’t have a concept of references so bothModelandBackbone.Modelpoint to the exact same object.