[thelist] javascript syntax

David Andersson Liorean at user.bip.net
Sat May 11 11:07:01 CDT 2002


At 00:52 2002-05-11 -0700, .jeff wrote:
>what's wrong with simply testing for the substring "mac" in
>navigator.platform like this?
>
>if(navigator.platform.toLowerCase().indexOf('mac') != -1)

It takes more code, and I'm a minimalist when it comes to what I put on the
web.

Take a look at the code:
   objref.toLowerCase().indexOf(strin)!=1
   regexp.test(objref)
There's some difference in length...

>surely that'll have *much* better support than a regular expression.

Not very much if you take into concern browsers in use. IE5.0 Mac and Win,
NN4, Moz/NN6, Opera, IceBrowser and others support it. In fact, I'd be as
concerned about navigator.platform not being supported on those browsers
that can't handle the regex.


>indexOf() is available as far back as js1.0 which covers pretty much every
>browser that's ever supported javascript.
>whereas test() using a regular expression is only available as far back as
>js1.2 which only makes up a marginal majority of browsers that support
>javascript and even then, it's support (as well as support for regular
>expressions in general) isn't consistent across those browsers that are

It's pretty consistent, at least when it comes to simple regexes such as
this one.


>[- - -]
>that doesn't make any sense to me at all, especially since the statement
>after the && operator isn't even a conditional but an statement of
>assignment (only one = in the statement).

It's a thing inherited from C/C++, IIRC. It's a space saver.
Conditional short-circuiting (by Netscape called short-circuit evaluation)
can be done through either OR (||) or AND (&&), with a bit of difference.
You see, since expressions on both sides of the AND needs to be true for
the entire statement to be true, the parser doesn't evaluate the right hand
expression if the left had expression is false.
Reversed thing goes for OR, if left hand side is true, it doesn't need to
evaluate the right hand expression.

You can use it instead of the conditional operator or an if statement in
some cases (I have examples where if statements would be a pita, but this
method makes it quite simple).

See
<http://developer.netscape.com/docs/manuals/js/core/jsguide15/expr.html#1008622>
or
<http://developer.netscape.com/docs/manuals/js/core/jsref15/ops.html#1044813>
or for the actual algorithm used, page 58 of the ECMA-262 3ed:
<http://www.mozilla.org/js/language/E262-3.pdf>


>prefacing the location with a slash works in every browser i've ever
>encountered.

Have you tested it in all varieties of NN4? I've found all three behaviors
in different versions of that browser.

// Liorean




More information about the thelist mailing list