shorthand syntax reference?

T

töff

Usually I write something like ...

if (x==1) { doSomething() }

.... and that's how I see it in all the JS references I've found so
far.

But I have seen shorthand (?) for the same thing (?), like this ...

(x==1) ? doSomething()

.... and several other "alternate" methods.

Are they documented somewhere? I kinda like them, but are they
supported in the usual gang of browsers? or is it stupid to use them
because they are fringe/sloppy?

Thnx in advance :)
 
E

Evertjan.

scripts.contact wrote on 15 apr 2007 in comp.lang.javascript:
condition?expression1:expression2

The operator _returns_ expression1 if the condition is true,
otherwise it returns expression2.

True, but disregarding the use of,
or in absense of a result value,
it can be and often is used
simply to execute one of those expressions:


(a>27.3) ? alert('high') : alert('low');
 
Y

Yorick

"Evertjan." schreef
simply to execute one of those expressions:

(a>27.3) ? alert('high') : alert('low');

Simpler still, in this case, unless one of those
statements/expressions/values is likely to change from an alert to a confirm
or prompt or anything else, is of course:

alert( a>27.3 ? 'high' : 'low' );

hth
Yorick
http://www.yorick.onlyfools.com/
 
E

Evertjan.

Yorick wrote on 15 apr 2007 in comp.lang.javascript:
"Evertjan." schreef

Simpler still, in this case, unless one of those
statements/expressions/values is likely to change from an alert to a
confirm or prompt or anything else, is of course:

alert( a>27.3 ? 'high' : 'low' );

Sure, but that was not the point.

The point was to show a tertiary choosing between functions without result.

Jammer dan.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
But I have seen shorthand (?) for the same thing (?), like this ...

(x==1) ? doSomething()

... and several other "alternate" methods.

Are they documented somewhere? I kinda like them, but are they
supported in the usual gang of browsers? or is it stupid to use them
because they are fringe/sloppy?

For such questions, you should look in ISO-16262, which should be linked
from the FAQ; failing that, try ECMA-262.

Such standards rarely contain the sentence-terminating question mark of
doubt as such, so a search for the character will be easy enough.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sun, 15 Apr 2007 03:13:04, scripts.contact
condition?expression1:expression2

The operator _returns_ expression1 if the condition is true,
otherwise it returns expression2.


http://msdn2.microsoft.com/en-us/library/be21c7hw.aspx

That document describes Jscript, and includes "This page is specific to
..NET Framework 3.0".

That site is not therefore a reliable guide for those writing javascript
for the Web; for that, you want a site describing standards which all
browsers can be expected to aspire to agreeing with.

But in this case it is not misleading.
 
E

Evertjan.

Ed Jay wrote on 15 apr 2007 in comp.lang.javascript:
Is there a level of nested instructions, independent of easing code
review, that is recommended against?

In general there is not, methinks, up to the amount of memory the
instruction stack takes or the time the instruction sequence takes before a
implemented timeout. Also a integer counter in your code, needed for
handling such deep nesting, or result variable, could overflow.

Try a reentrant faculty(n) code.

Specifically, as it is javascript we are discussing and not a specific
interperting engine, there could be implementation dependent limitations.
In your example above, 27.3 is
the result of a complex calculation, etc. Aside from reading ease, is
there any reason one should avoid lengthy, one-line, all-inclusive
instructions, e.g., including all the code that resulted in 27.3?

Next to reading ease, also debugging and error line reporing ease,
methinks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top