AW: AW: [thelist] javascript detecting if function/var is already defined
christof.hoeke at e-7.com
christof.hoeke at e-7.com
Fri Nov 28 05:48:09 CST 2003
Just a test:
var x = 'x';
(function(){
var
x;
return [x,
x == undefined,
typeof x];
})(); // => [ , true, 'undefined' ]
Nope, that does not seem to work. The reason? Have a look at the
following list:
1. Determine if member exists on an object:
//(it may exists but be undefined)
'x' in oParent // => boolean
2. Check what type the variable is:
typeof x // => string:
// 'undefined'|'object'|'number'|'boolean'|'function'|'string'
// ('undefined' is the result if the variable is not defined,
// independently of whether it exists or not)
3. Determine if the variable is undefined
// (assumes the existense of the variable in the first place)
x != undefined // => boolean
4. Determine if variable autocasts to true, variants
Boolean(x) // => boolean
!x // => boolean (inverted)
x || false // => anything (but false if it autocasts to false)
--
liorean <mailto:liorean at user.bip.net>
-----Ursprüngliche Nachricht-----
Von: liorean [mailto:liorean at f2o.org]
Gesendet: Freitag, 28. November 2003 12:35
An: thelist at lists.evolt.org
Betreff: Re: AW: [thelist] javascript detecting if function/var is
already defined
however it does seem to work on the top level:
a = 2
var a
if (a == undefined) alert("a is undefined");
else alert("a = " + a)
var b
if (b == undefined) alert("b is undefined");
else alert("b = " + b)
=> a = 2
and b = undefined
but i have to admit that typeof seems more right.
chris
More information about the thelist
mailing list