2012-03-23 75 views
3

可能重复:
Empty for loop - for(;;)为什么for(;;){...}是一个无限循环?

我刚刚发现UglifyJS的JS解析器一个奇怪的结构(在L1045):for(;;){…}

我认为一个空的条件会解析为undefined,它被转换为布尔值false。但事实绝非如此。

显然,它会触发一个无限循环。我能够重现这种行为,但我不知道为什么。任何(逻辑)解释?

此外:当这是可能的,为什么不while(){…}工作?

回答

4

这只是语义的定义。将缺少的“测试”表达式视为值为true的表达式。语言由人组成,他们可以随意指定他们喜欢的任何行为。很显然,这种行为是值得艾希先生喜欢:-)

+3

下面是关于此的规范:http://es5.github.com/x12.html#x12.6.3 – 2012-03-23 12:27:31

+0

哦,谢谢它永久加载我,出于某种原因:-) – Pointy 2012-03-23 12:29:16

+0

** + 1 * Eich先生...... :) – gdoron 2012-03-23 12:29:16

4

for(;;){…}解译空状态作为truewhile(){}不被视为有效。如前所述,它完全依赖于语言,但在说明书中进行了描述。

1

ECMA-262 language specification of JavaScript(第12.6.3节)中定义了for循环的行为应如何。

从定义中可以看出,如果分号周围和分号之间的信息不可用,则没有条件离开循环。离开循环的唯一方法是定义一个测试条件以及可选的一些开始和步骤值。

行为可以用不同的方式定义,但这不是它是如何定义的。

1

来自spec

12.6.3 The for Statement 
    The production 
     IterationStatement : for (ExpressionNoIn(opt) ; Expression(opt) ; Expression(opt)) Statement 
    is evaluated as follows: 
    1. If ExpressionNoIn is present, then. 
     a. Let exprRef be the result of evaluating ExpressionNoIn. 
     b. Call GetValue(exprRef). (This value is not used but the call may have side-effects.) 
    2. Let V = empty. 
    3. Repeat 
     a. If the first Expression is present, then 
      i. Let testExprRef be the result of evaluating the first Expression. 
      ii. If ToBoolean(GetValue(testExprRef)) is false, return (normal, V, empty) . 
     b. Let stmt be the result of evaluating Statement.© Ecma International 2011 91 
     c. If stmt.value is not empty, let V = stmt.value 
     d. If stmt.type is break and stmt.target is in the current label set, return (normal, V, empty) . 
     e. If stmt.type is not continue || stmt.target is not in the current label set, then 
      i. If stmt is an abrupt completion, return stmt. 
     f. If the second Expression is present, then 
      i. Let incExprRef be the result of evaluating the second Expression. 
      ii. Call GetValue(incExprRef). (This value is not used. 

吉斯特该规范的:for语句当一个表达式返回 “falsey” 值停止。由于没有表达式不返回false,脚本将永远运行(或直到break语句从循环体内执行)。