Re: isElement - determining if an object is an element
- From: "Aaron Gray" <ang.usenet@xxxxxxxxx>
- Date: Thu, 31 Jul 2008 17:33:37 +0100
"Henry" <rcornford@xxxxxxxxxxxxxx> wrote in message
news:9fc946c4-b316-4676-a2b5-4cbf0dd3e8e6@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jul 31, 4:07 pm, Aaron Gray wrote:
<snip>
return o && 'nodeType' in o && o.nodeType === 1;
Don't do this in preference to your previous version. The type-
converting to boolean test on - o - only guarantees it has trueness,
not that it is an object. If the right hand side of an - in -
operation is not an object an exception is thrown. Thus if - o - is a
primitive value that has truness avoidable exceptions will be thrown
in this code rather than false values returned (note that - instanceof
- can handle a primitive type left hand side operand). But once - o -
has been established as having tureness the expression - o.nodeType -
will not fail due to - o - being a primitive value, as it implies
internal type-conversation of the primitive into the corresponding
object (none of which will have - nodeType - properties).
Opps, not looking what I am doing there, yes I need to sit back and review
this whole thing properly rathr than just responding to the small peices,
badly.
<snip>
isElement(document.createElement('div')); // true in IE and FF
isElement({ nodeType: 1 }); // true in IE, but false in FF
Yes this is the sort of behaviour that I was trying to avoid,
hence adding in the 'typeof o.tagName === string' to make
the test stricter.
If IE had followed the other browsers and made browser types
first class we would not have these problems.
No, you would still have precisely this problem because javascript
objects can still be created to fool - o instanceof Element - into
returning a true result for an object that is not a DOM Element.
Yes I would not be too bothered about that, its a real side case if it
exists.
How do you do that anyway ?
Thanks Henry,
Aaron
.
- Follow-Ups:
- 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
- Re: isElement - determining if an object is an element
- From: Aaron Gray
- Re: isElement - determining if an object is an element
- From: Henry
- isElement - determining if an object is an element
- Prev by Date: Re: isElement - determining if an object is an element
- Next by Date: Re: isElement - determining if an object is an element
- 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):