Groups | Blog | Home
all groups > flash actionscript > may 2004 >

flash actionscript : testing for typeof(undefined)


sneakyimp
5/18/2004 9:08:40 PM
can anyone tell me why these both return false
if (typeof(undefined) == undefined) {
trace('yes we can trap it!');
} else {
trace('false');
}
if (typeof(undefined) === undefined) {
trace('yes 2 trap it');
} else {
trace('false');
}

when this returns undefined
trace(typeof(undefined));
Jack.
5/18/2004 9:27:15 PM
if (typeof(undefined) == "undefined") {
trace('yes we can trap it!');
}

sneakyimp
5/18/2004 9:47:42 PM
Jack.
5/18/2004 9:57:48 PM
AngerBeaver
5/19/2004 12:22:10 AM
sneakyimp
5/19/2004 12:29:22 AM
i agree that it's weird, but maybe you can follow this logic:

the typeof() function by definition returns a STRING.
despite the fact that the documentation (which really sucks) doesn't mention
that a possible return value of typeof() is "undefined", it is consistent that
if the object in question is a movie clip and "movieclip" is returned, "string"
is returned for strings, and so on, that if the type were in fact undefined,
that a string value "undefined" should be returned--as opposed to some
undefined value which would imply that typeof() failed or somehow could not
discern the actual type of the argument.


mandingo
5/19/2004 12:35:28 AM
Yes, the answer is simply that... typeof() returns a string so you need to test for "undefined" as a string because undefined and "undefined" are not necessarily the same thing.

AddThis Social Bookmark Button