I'm trying to understand the fundamentals of object literals
window.Test = ({
attribute1:'This is',
attribute2:'a test',
function1: function(){
alert(this.attribute1 + ' ' + this.attribute2);
}
});
//TEST 1
Test.function1(); // "This is a test"
//TEST 2
var tester = new Test(); //ERROR: Object is not a function
tester.function1();
If I understand correctly window.Test =
creates a global variable in the window object.
It would be the same as var Test =
. Therefore it's logical that TEST1 works.
In TEST2 I try to instantiate the literal but this is not possible because it's a variable. I got the idea from Backbone.js where the following code does work:
window.Product = Backbone.Model.extend({});
var p = new Product();
I would expect Product to be a variable here as well, so no way to instantiate it.
Can anyone explain the difference between the Backbone example and my example? It seems I'm missing out on some fundamental understanding of literals.