[...]
> > I agree with your sentiment that treemenu.js is crap, e.g.:
>
> > typeof(input[i]) == 'array'
>
> > Incidentally, jQuery had a similar test until very recently.
>
> I'm yet to find a reliable method for determining if an object is an
> array.
The test is absolutely reliable for native objects - it will always
return false. The point is that typeof will never return the string
'array' for native objects, it demonstrates that the author of
treemenu.js doesn't know much about ECMA-262. The jQuery reference is
a bit of a snipe. ;-D
You might get a chuckle from this if you haven't seen it already:
<URL:
http://groups.google.com.au/group/comp.lang.javascript/tree/browse_frm/thread/6ea0ec35f290dd1b/249604095daa1a20?hl=en&rnum=81&q=typeof+array+jquery&_done=%2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthread%2F6ea0ec35f290dd1b%2Ff814db369484353c%3Fhl%3Den%26lnk%3Dgst%26q%3Dtypeof%2Barray%2Bjquery%26#doc_f814db369484353c
>
The use of () around input[i] is pointless and may lead some to assume
that typeof is a function. Putting brackets around the unary
expression following an operator is a silly habit as it can lead to
script errors, try:
++(1);
> Even the time-proven Object.isArray from prototype.js does
> "naive" decisions based on object members.
"Time proven"? Yes, it's pretty simplistic and probably provides a
warm fuzzy feeling, but what does it actually prove?
> There's no guarantee that
> object implements [[Put]]
Internal methods can't be accessed directly using javascript and even
if they could, there'd have to be a way to distinguish between an
Array's special [[Put]] method and the one that might be implemented
by other objects.
The notion of internal methods is adopted by the specification purely
for the sake of describing behaviour (ECMA-262 8.6.2). It doesn't
mean that such a method exists in any implementation, only that
objects that are specified as having it behave as if they do.
> and has an expected "length" behavior.
The special length behaviour is a consequence of the special [[Put]]
(ECMA-262 15.4.5.1). Testing the behaviour of the length property
requires use of other methods that implement [[Put]] such as push, so
they would have to be tested first.
This leads to the strategy of testing those methods you are going to
use, implementing alternatives for those that are missing and ignoring
what you don't need.
In the case of treemenu.js, a simple test would be for the object's
constructor property:
if (x.constructor === Array) {
// do stuff...
}
which is not suitable in a general case but is is likely sufficient in
this one.
> > Also "Ultimate client-side JavaScript client sniff" (apparently last
> > updated in 2001) seems to think there are only 5 browser families,
> > none of which are Safari. A good example of why browser sniffing is a
> > flawed strategy.
>
> > > I remember Safari had issues with document being served with content-
> > > type: xhtml/application
>
> > That the browser is Safari is relevant in that the "Ultimate ...
> > client sniff" doesn't attempt to detect it (since the script appears
> > to have been last updated a couple of years before the first Safari
> > public beta), so it probably thinks Safari something else and responds
> > accordingly.
>
> > All browsers have their foibles: early versions of Safari certainly
> > had their fair share (and then some) but it's getting better in leaps
> > and bounds. In regard to serving XHTML as application/xhtml, any
> > browser that doesn't know what XHTML is will have a problem with that
> > - try it with IE. :-)
>
> Here's a relevant ticket that we tried to investigate some time ago:
http://dev.rubyonrails.org/ticket/9798
> Here's an actual WebKit bug mentioned in a ticket:
https://bugs.webkit.org/show_bug.cgi?id=15467
I didn't intend starting a discussion of browser bugs but to point out
that the script is incompatible with XHTML documents, hence the
usefulness of mentioning bugs related to XHTML is moot. Further, the
script uses browser detection that doesn't even know Safari exists and
so is unlikely to make any accommodation for its eccentricities.
--
Rob