我有一个网页,我想将一些自定义属性附加到全局窗口对象。我注意到在调试代码时我的控制台输出了我已经分配给窗口对象的属性>>实际分配了它。执行顺序如下:
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"
我无法使用普通的Javascript对象重现此行为。例如:
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"}
这背后的逻辑是什么?