开发者

two javascript syntax problem

These 开发者_开发百科days I am reading some javascript source codes,however I found some syntax that I can not understand.

1)the for-in loop

var c;
var obj={name:'test',age:33};

for(var e in c={},obj){
    console.info(e+' = '+obj[e]);
}

2)The Conditional Operator (?:)

Generally,we use this operator this manner:

x > 0 ? x*y : -x*y

But I have seen some codes like this:

x > 0 ? (x*y,z=bar,..other expressoin) : (-x*y)

But it does not work if I change the comma to colon,it will throw a error.


In both cases, the comma operator [MDN] is used:

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

And the specification:

11.14 Comma Operator ( , )

Syntax

Expression :
    AssignmentExpression
    Expression , AssignmentExpression

(...)

Semantics

The production Expression : Expression , AssignmentExpression is evaluated as follows:

  1. Let lref be the result of evaluating Expression.
  2. Call GetValue(lref).
  3. Let rref be the result of evaluating AssignmentExpression.
  4. Return GetValue(rref).

That just means that the result of the last expression is returned as result of the whole "list" of expressions.

In the examples you gave, it is used for its side effects [Wikipedia], namely evaluating every expression.
In general I'd say that this is not such a good style and, as you noticed, more difficult to understand.

for(var e in c={},obj)

is the same as

c = {};
for(var e in obj)

an does not seem to add any value. Even better would have been to just initialize c in the first line: var c = {};.

In case of the conditional operator: If x > 0 is true, then all the expressions are evaluated and the result of the last expression is returned. In this case, using a normal if statement would be better (easier to understand).

Here again, the comma operator and maybe even the conditional operator seems to be used solely because of their side effects: Normally the conditional operator is supposed to only return a value but not to execute arbitrary expressions (a single function call which returns a value might be an exception).


As the MDN documentation says, it is more commonly used in a for loop to initialize multiple variables:

for(var i = 0, l = array.length; i < l; i++)


In Javascript you can define variables like:

var obj1 = { a: 2, b: 4, c: 6};
var obj2 = { x: 1, y: 3, z: 5};

Or, you can comma-separate the declarations, like this:

var obj1 = { a: 2, b: 4, c: 6}, obj2 = { x: 1, y: 3, z: 5};

Now, a for in loop is normally something like:

for(var key in obj1) { console.log(key); }

But, if you comma-chain the part after 'in' it will allow it (just like when assigning), but will run the loop on the last object in the chain.

var key, obj1 = { a: 2, b: 4, c: 6}, obj2 = { x: 1, y: 3, z: 5};

// will output 'a', 'b' and 'c'
for(key in obj2, obj1) { console.log(key); }

// will output 'x', 'y' and 'z'
for(key in obj1, obj2) { console.log(key); }

This means you can assign values within the for in, and then do looping on another object.

var key, obj3; // obj3 === null

var obj1 = { a: 2, b: 4, c: 6}, obj2 = { x: 1, y: 3, z: 5};

// will output 'a', 'b' and 'c'
for(key in obj3 = { j: 9, k: 8, l: 7 }, obj1) { console.log(key); }

// obj 3 is now { j: 9, k: 8, l: 7 }

Okay, so now for the ternary operators. If the above makes sense, so should this.

Ternary operations are like if statements with a single line. You can't put semicolons in a line because then it becomes two or more lines. But putting () around comma-separated declarations then it is still considered one line.

So this is valid:

x > 0 ? a = 1 : a = 2;

And this is valid:

x > 0 ? (a = 1, b = 2) : (a = 2, b = 1);

But this is not, because the commas throw off the interpretation of the ternary:

x > 0 ? a = 1, b = 2 : a = 2, b = 1;

But I would not recommend setting values in a ternary, unless it looks something like:

a = x > 0 ? 1 : 2;

:)


The first? no idea. The second? It's just like your typical inline if except it executes multiple expressions rather than one

x > 0 ? (doThis(),doThat(),x=2) : (again(),another(),y=5);


Isn't the first one just an initializer?

var c;
var obj={name:'test',age:33};

for(var e in c=[], obj){
    console.info(e + ' = ' + obj[e]);
    c.push(e);
}

console.info(c);

Now, why you'd do it that way... I'm not sure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜