First of all, I’m a JavaScript noob and even noober in Backbone.js.
I’m following some online tutorials to get familiar with the technology; and while in some tutorials I saw this:
Person = Backbone.Model.extend({
initialize: function(){
alert("Welcome to this world");
}
});
var person = new Person;
In other ones I found this notation:
window.WineView = Backbone.View.extend({
initialize:function () {
this.model.bind("change", this.render, this);
}
});
What’s the necessity of adding “window” to the View name? I read some JavaScript documentation that states that the window object represents the current browser window, so are we adding our created view as an attribute of this object? Why is the author doing that?
In browser JavaScript,
windowis the global object, sowindow.Person === Personunless there’s aPersondefined somewhere in the scope chain (function() { var Person; ... }).In other words, there is no difference. (Other than using 7 extra characters.)
You might want to read up on JS scope. Specifically, if you assign to an otherwise undefined variable (
Person = { ... };), that variable will be assigned to the global object (window). However, if you first declare a variable (var Person = { ... };), the variable is assigned to local scope.