Javascript - 嵌套for循环索引的范围 [英] Javascript - scope of nested for loop index

查看:112
本文介绍了Javascript - 嵌套for循环索引的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得变量是Javascript中的函数作用域。但是,如果我在循环中重新定义局部变量,行为如何。一个常见的用例是嵌套循环。
在下面的代码中,如果我将j更改为i,则外部for循环在一次迭代后终止,因为外部作用域中的i值与内部for循环相同。由于我使用var,我期待(类似于其他语言)它在内部fo循环中被重新定义。这是否意味着在JS中,没有办法在函数范围内重新声明和使用局部变量。

I remember variables are function scoped in Javascript. But, how is the behavior if I redefine the local variable in a loop. One common use case is nested loops. In the below code, if I change j to i, the outer for loop terminates after one iteration as the value of i in outer scope is same as inner for loop. Since I use var, I was expecting (similar to other language) it is redefined inside inner fo loop. Does this mean in JS, there is no way to redeclare and use local variable within function scope.

for (var i = 0, len = x.length; i < len; i++) {
            ...
            for (var j = 0, len = y.length; j < len; j++) {
                ...
            }
        }


推荐答案

正如你所说,JavaScript只有功能范围。变量声明被提升到声明它们的作用域的顶部。您的示例解释如下:

As you said, JavaScript only has function scope. Variable declarations are hoisted to the top of the scope in which they are declared. Your example is interpreted like this:

var i, j, len; //Declarations are hoisted...
for (i = 0, len = x.length; i < len; i++) { //Assignments happen in place
    for (j = 0, len = y.length; j < len; j++) {

    }
}

至于这一部分:


如果我将j更改为i,则外部for循环在一次迭代后终止

if I change j to i, the outer for loop terminates after one iteration

如果用 i j c>,然后在内循环的第一次迭代之后, i 将是 y.length - 1 ,并且外部循环将继续或停止,具体取决于 x.length y.length 之间的差异。

If you replace j with i, then after the first iteration of the inner loop, i will be y.length - 1, and the outer loop will either continue or stop, depending on the difference between x.length and y.length.

如果您对内部工作的真实解释感兴趣, ECMAScript规范(声明绑定实例)详细介绍了它。总而言之,每次控件进入新的执行上下文时,都会发生以下情况(比这更多,但这是其中的一部分):

If you're interested in the real explanation of the internal workings, the ECMAScript spec (Declaration Binding Instantiation) covers it in detail. To summarise, every time control enters a new execution context, the following happens (a lot more than this happens, but this is part of it):


对于代码中的每个 VariableDeclaration VariableDeclarationNoIn d ,在
源文本顺序中执行

For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do


  • dn 成为 d 中的标识符

  • varAlreadyDeclared 成为调用 env的 HasBinding具体
    方法传递 dn 的结果参数。

  • 如果 varAlreadyDeclared 为false,则

    • 调用 env's CreateMutableBinding具体方法传递 dn
      configurableBindings 作为参数。

    • 调用 env的 SetMutableBinding
      具体方法传递 dn ,undefined和 strict 作为参数。

    • Let dn be the Identifier in d.
    • Let varAlreadyDeclared be the result of calling env’s HasBinding concrete method passing dn as the argument.
    • If varAlreadyDeclared is false, then
      • Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.
      • Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.

      这意味着如果每个执行上下文多次声明一个变量,它将被有效地忽略。

      This means that if you declare a variable more than once per execution context, it will effectively be ignored.

      这篇关于Javascript - 嵌套for循环索引的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆