It appears that window.undefined
is writable, i.e. it can be set to something else than its default value (which is, unsurprisingly, undefined
).
The point is however that whenever I refer to undefined
, it refers to window.undefined
(as window
can be removed in cases like this).
So how do I actually get access to an undefined
'instance', so to say? How would I be able to set another variable to undefined
, if window.undefined
has been changed?
If I code:
window.undefined = 'foo'; // This code might have been executed by someone/something
var blah = undefined; // blah is not undefined, but equals to 'foo' instead...
How could I possibly solve this?
It appears that window.undefined
is writable, i.e. it can be set to something else than its default value (which is, unsurprisingly, undefined
).
The point is however that whenever I refer to undefined
, it refers to window.undefined
(as window
can be removed in cases like this).
So how do I actually get access to an undefined
'instance', so to say? How would I be able to set another variable to undefined
, if window.undefined
has been changed?
If I code:
window.undefined = 'foo'; // This code might have been executed by someone/something
var blah = undefined; // blah is not undefined, but equals to 'foo' instead...
How could I possibly solve this?
Share Improve this question asked Feb 16, 2011 at 19:26 pimvdbpimvdb 155k80 gold badges311 silver badges356 bronze badges 7 | Show 2 more comments4 Answers
Reset to default 19The "standard" solution to this problem is to use the built in void
operator. Its only purpose is to return undefined:
var my_undefined = void 0;
In addition to this, yhere are other ways to get undefined
:
Functions return undefined if you don't return
anything so you could do something like
this_is_undefined = (function(){}());
You also get undefined if you don't pass enough arguments to a function. So a common idiom is
function foo(arg1, arg2, undefined){ //undefined is the last argument
//Use `undefined` here without worrying.
//It is a local variable so no one else can overwrite it
}
foo(arg1, arg2);
//since you didn't pass the 3rd argument,
//the local variable `undefined` in foo is set to the real `undefined`.
This kind is particularly good for cases when you define and call the function at the same time so you don't have any risk of forgetting and passing the wrong number of arguments latter.
In addition to the other solutions, you can do the void 0
trick, which always returns undefined
irrespective of the window
property.
window.undefined = 'foo';
var blah = void 0;
alert( blah ); // undefined
Actually, comparing anything with undefined
is not good idea at all. Should use typeof
operator instead:
function isUndefined ( variant ) { return typeof variant === 'undefined' }
It's should be enough to just declare a variable without assigning it to anything:
var local_undefined;
alert(typeof local_undefined);
But why on Earth is it possible to change undefined value? Does anyone know history behind this?
undefined
? – John Kugelman Commented Feb 16, 2011 at 19:29var undef = window.undefined; window.undefined='foo'; car myvar = undef;
– Brad Christie Commented Feb 16, 2011 at 19:29This code might have been executed by someone/something
. Where to take the valueundefined
from then? – Martin Hennings Commented Feb 16, 2011 at 19:31window.undefined
. E.g., someone else could setwindow.undefined
and then my code gets executed. Then is there any way to still getundefined
? – pimvdb Commented Feb 16, 2011 at 19:31undefined
so this could not be a case then. – Pawel Sledzikowski Commented May 14, 2013 at 10:35