Re: isElement - determining if an object is an element



On Jul 31, 11:07 am, "Aaron Gray" <ang.use...@xxxxxxxxx> wrote:
"kangax" <kan...@xxxxxxxxx> wrote in message

news:43fd545a-5e4c-4df8-a951-f8cb04ede4f6@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jul 30, 11:52 am, "Aaron Gray" <ang.use...@xxxxxxxxx> wrote:

[snip]
The double function expression is unnecessary.

It could be rewritten like so:

var isElement = (function(){
 var el = document.createElement('div'), fn;

   if (Element && el instanceof Element) {  // Thanks RobG>    fn = function(o) {
     return o instanceof Element;
   }
 }
 else {
   fn = function(o) {

      return o && 'nodeType' in o && o.nodeType === 1;

   }
 }
 el = null;
 return fn;
})();

We don't perform such branching in prototype.js to avoid
inconsistencies across browsers.

LOL.


Right. Is the inconsistancy really that bad as to cause problems with an
example like this ?

You and the Prototype guy are both trying to solve the same problem
that does not need to be solved. Nobody need a GP function to
discriminate between an Object object and an element.


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.

We don't have a problem. It is people who wish to make the language
work more like Java who have a problem. The problem is easily
diagnosed: they do not understand the language they are using or
browser scripting in general.
.



Relevant Pages

  • Re: FAQ and JavaScript vs. Ruby (was Re: rubynuby - client side
    ... Minor implementation inconsistencies between browsers ... Just plugging Ruby into browsers wouldn't really solve the API problems. ...
    (comp.lang.ruby)
  • Re: langua= vs type=
    ... mark-up through a validator, and validation as having the validator ... There is a risk that individuals using the language ... > 2) Older browsers may not understand the "type" attribute. ... why you can get away with just <SCRIPT>. ...
    (comp.lang.javascript)
  • Re: How to get the absolute position after scroll
    ... The LANGUAGE attribute is valid HTML. ... It is a deprecated attribute in HTML 4 and as such cannon be used with the 'strict' DTD and result in a valid HTML document. ... even browsers that do pass an event object when they call the even handling function also provide a global 'event' property to refer to the event. ...
    (comp.lang.javascript)
  • Re: FAQ Update 9.85 Dated 2007-08-31
    ... browsers, I concurred if that book was named "ECMAScript Implementations: ... language, it is an execution environment for the latter programming language. ... deviation can be handled easily by avoiding the trailing comma. ...
    (comp.lang.javascript)
  • Re: Baffling Results from my Javascript Class(-ishness) (Ninjas explain!)
    ... If you want to take it seriously as a language, ... But the typical users of these libraries don't ... If you twiddle with a script long enough, ... a handful of browsers. ...
    (comp.lang.javascript)