Dr John Stockton <sp**@merlyn.de mon.co.uk> writes:
JRS: In article <bp************ *@ID-174912.news.uni-berlin.de>, seen in
X ? Y : Z means if X then Y else Z
So this is exactly equivalent to:
if (X) { Y }
else { Z }
No; but I think the converse is true. The first is a superset of the
second, since it can be used where a value is required; A=X?Y:X
It depends on what Y and Z are. If they can stand for arbitrary
Javascript syntax, then it isn't true. You can't put statements into
the ?:-operator. If Y and Z are just variables, then they are expressions,
and can be used in both places (expressions can be used as statements,
although using an expression with no side effect is pointless).
I.e., there are four different things that can vary:
- use ?: or if-then as conditional branch
E.g., x?...:... or if(x) ... else ...
- have expressions or statements in the branches
E.g., 2+2 (expression) or return 2+2; (statement)
- use the value of the conditional branch or not
E.g., q=(conditional branch); or (conditional branch);
- have side effects in the branches or not
E.g., w=2+2/foo() or 2+2
That give 16 different cases, many of them invalid.
It is invalid to use statments in the branches of the ?: operator.
It is invalid to use the value of an if-statement.
It is meaningless to not have side-effects in an if-statment
Without side effects in branches:
As a statement (not using value):
if (x) {2+2;} else {4+4;} : valid but pointless
if (x) {return 2+2;} else {return 4+4;} : valid and meaningful
x?(2+2):(4+4) : valid but pointless
x?(return 2+2;):(return 4+4;) : invalid
As an expression (value is used):
q = if (x) {2+2;} else {4+4;} : invalid
q = if (x) {return 2+2;} else {return 4+4;} : invalid
q = x?(2+2):(4+4) : valid and meaningful
q = x?(return 2+2;):(return 4+4;) : invalid
With side effects:
As a statement:
if (x) {w=2+2;} else {foo();} : valid and meaningful
if (x) {return w=2+2;} else {return foo();} : valid and meaningful
x?(w=2+2):(foo( )) : valid but you should use if
x?(return w=2+2;):(return foo();) : invalid
As an expression (value is used):
q = if (x) {w=2+2;} else {foo();} : invalid
q = if (x) {return w=2+2;} else {return foo();} : invalid
q = x?(w=2+2):(foo( )) : valid and meaningful
q = x?(return w=2+2;):(return foo();) : invalid
Hope this makes any sense :)
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'