[thelist] JS: compound ternary

liorean liorean at f2o.org
Sat Jan 31 12:20:13 CST 2004


Tom Dell'Aringa wrote:
> Hey JS people,
> 
> I'm trying to build a compound ternary statement and I keep falling
> flat on my face. I know I have something mixed up, I keep getting a
> syntax error. There's a bracket set missing or something. Here's the
> statement:
> 
> var restrict = (document.getElementById) ?
> document.getElementById(oName) : (document.all) ? document.all[oName]
> : return;
> 
> The first condition should check for get by ID, next doc.all. If its
> not either it should return, otherwise is should set the correct
> object reference. I know you can do this I've seen it many times, I
> just can't find an example when I need it (as usual).

Operators - including the conditional* operator - operate on
expressions. Expressions execute in context and always returns a value,
and returns the execution  to the context they were called in. Return is
a statement. Statements are executed in a context, but are context
independent. They never return a value and does not always return
execution to the context in which they were run.

Because of this, you may never place a statement in a place where you
expect an expression, while you may always place an expression where you
expect a statement.

(Ternary only tell you how many expressions it operates on. Conditional
is the name of it. The only reason you get away with calling it the
ternary operator is that it's the only ternary operator in JavaScript.)

-- 
liorean <mailto:liorean at user.bip.net>

ViewStyles, ViewScripts, ToggleStyles and GraphicsInfo bookmarklets and
Theme Switcher, Cookies Handler scripts:
<http://liorean.web-graphics.com/>




More information about the thelist mailing list