arm.localhost
arm.localhost

Reputation: 479

Redefining `undefined` in `window` object

I wonder is it possible to create function that would have a name "undefined" in order not get error when calling undefined()?

I've tried to do this way

var obj = {};
obj[undefined] = function() {};
obj.undefined();

It works, however, I want to do something like this:

window[undefined] = function() {};

But my browser (Chrome 44) doesn't allow to do it.

Any ideas?

Upvotes: 2

Views: 991

Answers (1)

thefourtheye
thefourtheye

Reputation: 239483

Because undefined is a non-writable property of window object.

Object.getOwnPropertyDescriptor(window, "undefined").writable
false

Since it is not writable, it simply ignores the write operation.

Quoting MDN documentation on undefined,

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.

Upvotes: 2

Related Questions