Justin said:
javascript version of PHP's isset would be:
typeof(xyz)!='undefined'
No, because PHP's isset()
,-<
http://php.net/isset>
| Returns TRUE if var exists; FALSE otherwise.
"Exists" has to be read as "has been defined, its value is not NULL and
unset() was not applied on it.":
,-<ibid.>
| If a variable has been unset with unset(), it will no longer be set.
| isset() will return FALSE if testing a variable that has been set to
| NULL.
But in JS, there are fundamental differences:
1. An already instantiated variable may have/become the
value of `undefined':
var x;
alert(typeof x); // `undefined'
alert(x); // `undefined' or nothing
var y = y;
alert(typeof y); // `undefined'
alert(y); // `undefined'
2. An already instantiated variable may be undefined by
having the `delete' operator applied on it, provided
that it was not declared (`var' keyword was not used):
z = 2;
alert(typeof z); // `number'
delete z;
alert(typeof z); // `undefined'
alert(z); // ReferenceError
3. A named function argument may be not supplied. In
that case, its value is, by definition
, `undefined':
function foo(a, b)
{
alert([typeof a, typeof b]); // `number,undefined'
}
foo(42);
4. Although `null' in JS is a special value of the internal Null type,
,-<ECMAScript 3>
| 4.3.11 Null Value
| The null value is a primitive value that represents
| the null, empty, or non-existent reference.
|
| 4.3.12 Null Type
| The type Null has exactly one value, called null.
which it has in common with PHP (where NULL is the sole value of
the NULL type), it yields
var x = null;
alert(typeof x); // `object'
This is most certainly an attribution to the fact that a reference
always refers to an object or not.
Conclusion:
Besides risking a ReferenceError (and hopefully catching it with exception
handling), there is no way in JS to determine whether a variable/property
was defined or not; it is only possible to determine whether it yields
`undefined' (the sole value of the internal Undefined type) or `null', or
not.
PointedEars