G
Garrett Smith
A bit of advice here regarding the `in` operator. In a nutshell: Avoid
doing that.
Avoid the <code>in</code> operator for host objects. Host objects use a
yet-unstandardized "catchall" behavior to "catch" property names during
get access, returning the value. Unfortunately, implementations vary.
Even within one browser, two objects may have different type of catchall
behavior. When "get" catchall behavior is implemented and "has" catchall
is not, the result is an object that, to its user, appears inconsistent,
contradicting itself by reporting that it does not have the property
while returning the property on get access.
<http://wiki.ecmascript.org/doku.php?id=strawman:catchalls>
Catchall Example:
// In Firefox 3.6 and below:
var p = navigator.plugins, ss = "Shockwave Flash";
s in p; // false
p[ss]; // [object Plugin]
// In Firefox 3.6 and below:
var f = document.forms[0], ss = "0";
s in f; // false;
f[ss]; // [object HTMLInputElement]
// In IE 8 and below:
var s = document.styleSheets, ii = "9999";
i in s; // true;
s[ii]; // Error.
It is generally safer to use the typeof operator and compare the result
against undefined.
var hasP = typeof obj.prop != "undefined";
doing that.
Avoid the <code>in</code> operator for host objects. Host objects use a
yet-unstandardized "catchall" behavior to "catch" property names during
get access, returning the value. Unfortunately, implementations vary.
Even within one browser, two objects may have different type of catchall
behavior. When "get" catchall behavior is implemented and "has" catchall
is not, the result is an object that, to its user, appears inconsistent,
contradicting itself by reporting that it does not have the property
while returning the property on get access.
<http://wiki.ecmascript.org/doku.php?id=strawman:catchalls>
Catchall Example:
// In Firefox 3.6 and below:
var p = navigator.plugins, ss = "Shockwave Flash";
s in p; // false
p[ss]; // [object Plugin]
// In Firefox 3.6 and below:
var f = document.forms[0], ss = "0";
s in f; // false;
f[ss]; // [object HTMLInputElement]
// In IE 8 and below:
var s = document.styleSheets, ii = "9999";
i in s; // true;
s[ii]; // Error.
It is generally safer to use the typeof operator and compare the result
against undefined.
var hasP = typeof obj.prop != "undefined";