className: "messages-‐container" var messagesView = new MessagesView({ collection: mainMessages }); /* ... */ }); initialize: function() {/*...*/} 60 Saturday, June 16, 2012 So! I want to talk about constructors, prototypes, and instances. The headline is the short version and those of you who have done JavaScript for a long time are probably thinking, .. “thank you, captain obvious.” But I come from an object-oriented class-based language background - before I did Ruby, I did Java. In an object-oriented language, we create a ‘class definition,’ and then create instances of that class using that definition. JavaScript has prototypes instead, which feel a lot like classes but have some important differences. So here is the ‘class definition’ - that’s how I thought of it for a long time - that we wrote for our backbone MessagesView. After this statement runs, the var capital-M MessagesView is actually just an object, like any other JavaScript object, with attributes and values. It’s not a special class object, or anything wacky like that. It’s just an object. Prototyping means that to create “instances” of this “class,” JavaScript essentially just clones this object. This prototype is extending Backbone.View, which gives it a bunch of properties that we don’t explicitly specify, including one called initialize, which is a function. When we create a new instance of the view from this prototype object, that function is called. So here’s how we created a new instance of the MessagesView collection - using the new operator. When we did that, Backbone looked into the MessagesView prototype object, got its initialize function, and called that function passing in any arguments that we give to new. And what that did is basically clone the prototype and then apply any overrides that we pass in. Now the key is that we can use the arguments we pass in here, at the bottom, when we’re creating an instance, to override anything that is up here at the top, in the prototype. So up here, where we are creating the prototype, we set the stuff that is going to be true for pretty