Reputation: 47071
In browser, I tried to evaluate the following snippet:
window = 1; console.log(window)
The value printed in console is still the original window object instead of number 1
.
Does anyone have ideas about why window
object can't be re-assigned? Is this a feature of Javascript language? Can I make my own object not writable like window object too?
Upvotes: 3
Views: 567
Reputation: 32212
The specification says:
The
window
,frames
, andself
IDL attributes must all return theWindow
object's browsing context'sWindowProxy
object.
The "must" here implying that no matter what you do, every request to window
will result in the WindowProxy object.
Upvotes: 1
Reputation: 943981
It can't be overwritten because it is defined as being non-writable.
That is a feature of JS (although the window
object is probably implemented at a lower level).
var example = {};
Object.defineProperty(example, "foo", {
value: "Hello",
writable: false
});
document.body.appendChild(document.createTextNode(example.foo));
example.foo = "World";
document.body.appendChild(document.createTextNode(example.foo));
Upvotes: 3