Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `ExpressionNoIn` mean in the ECMAScript spec?

I'm doing a bit of a deep dive into the for loop and encountered ExpressionNoIn in the spec at http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.3

What does it mean?

like image 547
Derek Johnson Avatar asked Mar 27 '15 20:03

Derek Johnson


Video Answer


2 Answers

It is explained in section 11.14 "Comma Operator ( , )":

Comma Operator ECMA Spec

The *NoIn has the same structure, except that in excludes the use of the in keyword, section 11.8 "Relational Operators":

Relational Operators ECMA Spec

The spec says:

The "NoIn" variants are needed to avoid confusing the in operator in a relational expression with the in operator in a for statement.

As in may be used in two ways:

for (var x in foo) { ... }

Or:

if ('x' in foo) { ... }

The "NoIn" variants are there to make it impossible to use the second version of in above in the first expression of the for loop. So, the following code is invalid:

for (y = 'x' in foo; y; y = false) { ... }
like image 195
Thiago Negri Avatar answered Nov 14 '22 21:11

Thiago Negri


ExpressionNoIn is a non terminal from which all expressions can be derived, expect the in operation (i.e. 'prop' in obj).

Follow A3 from the bottom (where ExpressionNoIn is defined) to the first *NoIn non-terminal which does not contain a (different) *NoIn non-terminal anymore:

RelationalExpression :
    ShiftExpression
    RelationalExpression < ShiftExpression
    RelationalExpression > ShiftExpression
    RelationalExpression <= ShiftExpression
    RelationalExpression >= ShiftExpression
    RelationalExpression instanceof ShiftExpression
    RelationalExpression in ShiftExpression

RelationalExpressionNoIn :
    ShiftExpression
    RelationalExpressionNoIn < ShiftExpression
    RelationalExpressionNoIn > ShiftExpression
    RelationalExpressionNoIn <= ShiftExpression
    RelationalExpressionNoIn >= ShiftExpression
    RelationalExpressionNoIn instanceof ShiftExpression

I guess that makes it easier to distinguish for loops from for/in loops.

like image 32
Felix Kling Avatar answered Nov 14 '22 23:11

Felix Kling