I have a web page and I would like to attach some custom properties to the global window-object. I noticed when debugging the code that my console outputs the property I have assigned to the window object before it is actually assigned. The execution order goes as follows:
console.log(window); //Outputs window object with "myApp"-property with the value "something"
console.log(window.myApp); //Outputs "undefined"
window.myApp = "something";
console.log(window.myApp); //Outputs "something"
console.log(window); //Outputs window object with "myApp"-property with the value "something"
I cannot reproduce this behaviour with normal Javascript objects. For example:
var obj1 = {a: "bar"};
console.log(obj1); //Outputs Object {a: "bar"}. does NOT output obj1.b
obj1.b = "foo";
console.log(obj1); //Outputs Object {a: "bar", b: "foo"}
What is the logic behind this?