最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

complicated javascript conditional expression - Stack Overflow

programmeradmin0浏览0评论

What is the correct way to interpret this plicated javascript expression?

some_condition ? a = b : c = d = e;

Following operator precedence rules, I would expect it to be:

(some_condition ? a = b : c) = d = e;

But it seems that the grouping is actually:

EDIT: (The original grouping was unclear. See below for update)

EDIT: some_condition ? a = b : (c = d = e);

Why is this so? (And no I did not write that code)

EDIT: This seems to suggest that in Javascript to say ?: have higher precedence than = is not entirely true. As a further example:

x = y ? a = b : c = d = e;

If ?: have higher precedence than = (as in C) then the grouping would be

x = ((y ? a = b : c) = (d = e));

but rather (from the answers) what we have is

x = (y ? a = b : (c = d = e));

The relative precedence of ?: and = seems to depend on where they appear in the expression

What is the correct way to interpret this plicated javascript expression?

some_condition ? a = b : c = d = e;

Following operator precedence rules, I would expect it to be:

(some_condition ? a = b : c) = d = e;

But it seems that the grouping is actually:

EDIT: (The original grouping was unclear. See below for update)

EDIT: some_condition ? a = b : (c = d = e);

Why is this so? (And no I did not write that code)

EDIT: This seems to suggest that in Javascript to say ?: have higher precedence than = is not entirely true. As a further example:

x = y ? a = b : c = d = e;

If ?: have higher precedence than = (as in C) then the grouping would be

x = ((y ? a = b : c) = (d = e));

but rather (from the answers) what we have is

x = (y ? a = b : (c = d = e));

The relative precedence of ?: and = seems to depend on where they appear in the expression

Share Improve this question edited Apr 3, 2012 at 6:11 tyty asked Apr 2, 2012 at 15:43 tytytyty 8395 silver badges12 bronze badges 4
  • 1 Uh oh, better break out the spec again... – zzzzBov Commented Apr 2, 2012 at 15:47
  • That is because you haven't give brackets. Try giving brackets. It should work as expected. – sgowd Commented Apr 2, 2012 at 15:49
  • not really.. inline if does not require brackets or parenthesis. Check Simon West's answer below. – MilkyWayJoe Commented Apr 2, 2012 at 15:51
  • 1 It's interpreted as some_condition ? (a = b) : (c = d = e); – gen_Eric Commented Apr 2, 2012 at 15:52
Add a ment  | 

6 Answers 6

Reset to default 3

If you look at javascript precedence operators, assignments have a lower precedence than conditional operator but, according to ecmascript specs, Par. 11.12

11.12 Conditional Operator ( ? : )     
Syntax

ConditionalExpression : LogicalORExpression  
       LogicalORExpression ? AssignmentExpression : AssignmentExpression

AssignmentExpression is evaluated as follows:  
1.  Let lref be the result of evaluating LogicalORExpression.   
2.  If ToBoolean(GetValue(lref)) is true, then  
   a. Let trueRef be the result of evaluating the first AssignmentExpression.   
   b. Return GetValue(trueRef).  
...

So the conditional operator evaluates that code grouping every assignment expression and it works as everybody explained.

Maybe I could be wrong here, but I think that operators precedence are related instead to how js parse an expression like

a = (b < c)? b : c;

(and not when contained in the AssignmentExpressions) where in this scenario the assignment should have a lower precedence, so Javascript evaluates the whole statement as

a = ((b < c)? b : c);

Thats an abbreviated if else statement

the long form would be

if(some condition)
{
 a = b;
}
else 
{
 c = d = e;
}
if( some_condition ){
    a=b;
}else{
    d=e;
    c=d;
}

var result = true ? a=b : c=d=e;

if it is true a is equals to b and result equals b

var result = false ? a=b : c=d=e;

if it is false d equals to e, c also equals to e and result is e

You may also find this definition of the ternary operator helpful.

Usually that would be used for assigning something based on the condition, but this one seems to just be changing a or c and d based on the condition.

I frequently see things like val = isConditionTrue ? trueVal : falseVal; Then val can be either trueVal or falseVal based on isConditionTrue.

The Conditional Operator is defined as:

ConditionalExpression :
    LogicalORExpression
    LogicalORExpression ? AssignmentExpression : AssignmentExpression

a = b is an AssignmentExpression, and so is c = d = e.

发布评论

评论列表(0)

  1. 暂无评论