Re: isElement - determining if an object is an element
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Wed, 30 Jul 2008 22:28:55 -0700 (PDT)
On Jul 31, 2:33 pm, kangax <kan...@xxxxxxxxx> wrote:
[...]
It could be rewritten like so:
var isElement = (function(){
var el = document.createElement('div'), fn;
if (el instanceof Element) {
That causes IE8 Beta 1 to barf with "Element is not defined".
Modifying it to:
if (Element && (el instanceof Element))
or
if (Element) {
if (...) {
gets the same result, it wants a more explicit test:
if (typeof Element != 'undefined' && (...))
Perhaps that's documented on one of the IE8 blogs somewhere...
fn = function(o) {
return o instanceof Element;
}
}
else {
fn = function(o) {
return o && typeof 'nodeType' in o && o.nodeType === 1;
After applying your fix to the above to remove typeof, what is the
point of the test? Its use as a guard here seems unnecessary: if o
doesn't have a nodeType property, o.nodeType will return undefined and
the test fails (even in IE8b).
}
}
el = null;
return fn;
})();
We don't perform such branching in prototype.js to avoid
inconsistencies across browsers.
isElement(document.createElement('div')); // true in IE and FF
isElement({ nodeType: 1 }); // true in IE, but false in FF
But there you have an inconsitency, lending support to the argument
that feature detection is much better than inference (i.e. if you
expect an object to have a particular property, test for that
property, don't test it for some other property to infer that is has
the one you really want).
The instanceof test is open to the same criticism.
--
Rob
.
- Follow-Ups:
- Re: isElement - determining if an object is an element
- From: kangax
- Re: isElement - determining if an object is an element
- From: Lasse Reichstein Nielsen
- Re: isElement - determining if an object is an element
- References:
- isElement - determining if an object is an element
- From: Aaron Gray
- Re: isElement - determining if an object is an element
- From: Henry
- Re: isElement - determining if an object is an element
- From: Aaron Gray
- Re: isElement - determining if an object is an element
- From: Aaron Gray
- Re: isElement - determining if an object is an element
- From: kangax
- isElement - determining if an object is an element
- Prev by Date: Refreshing the parent page from the SECOND child
- Next by Date: Reading an array of numbers
- Previous by thread: Re: isElement - determining if an object is an element
- Next by thread: Re: isElement - determining if an object is an element
- Index(es):
Relevant Pages
|