javascript for 循环变量和递归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6419128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:39:48  来源:igfitidea点击:

javascript for loop variable and recursion

javascript

提问by mcot

I have an issue where I have recursion inside of a for loop:

我有一个问题,我在 for 循环内有递归:

function func(node) {
    for(var i = 0; i < node.children.length; i++) {
       func(node.children[i]);
    } 
} 

Obviously because JavaScript does not have block scope, the same i variable is getting modified each time the function is called. What is the best way to remedy this? Assume regular EcmaScript 3 and I can't use JavaScript 1.7 "let".

显然因为 JavaScript 没有块作用域,所以每次调用函数时都会修改相同的 i 变量。解决此问题的最佳方法是什么?假设常规的 EcmaScript 3 并且我不能使用 JavaScript 1.7“let”。

I know this has been asked before, but the other questions don't seem to show recursion, they show one function call where a closure could be used.

我知道以前有人问过这个问题,但其他问题似乎没有显示递归,它们显示了一个可以使用闭包的函数调用。

回答by MackPro

Cache the length of the array so you would have the following:

缓存数组的长度,以便您拥有以下内容:

function recurse(node) {
    for(var i = 0, count = node.children.length; i < count; i++) {
        recurse(node.children[i]);
    }
} 

You should always cache especially when you're dealing with HTMLCollections.

您应该始终缓存,尤其是在处理 HTMLCollections 时。

回答by ?ime Vidas

Just use Crockford's walkTheDOMfunction:

只需使用 Crockford 的walkTheDOM功能:

function walkTheDOM(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walkTheDOM(node, func);
        node = node.nextSibling;
    }
}

You pass in the root node and the function that you want to run for each node, like so:

您传入根节点和要为每个节点运行的函数,如下所示:

var root = document.getElementById('wrap');

walkTheDOM(root, function(node) {
    console.log( node.nodeName );
});

Live demo:http://jsfiddle.net/VKWTt/

现场演示:http : //jsfiddle.net/VKWTt/

回答by karthick-sk

Was this facing issue, like during recursion of a function the variable values got replaced. the recursion was inside forloop, so the variables inside the forloop where modified.

这是面临的问题,比如在函数的递归过程中,变量值被替换了。递归在for循环内,因此for循环内的变量被修改。

Use varto declare variables that are modified at recursion.

使用var声明在递归时修改的变量。

回答by Daniel Teichman

You've already defined 'i' as a variable in a broader scope ;)

您已经将 'i' 定义为更广泛范围内的变量;)

回答by Richard Schneider

I think you example should work. The variable iis declared local, so when you recurse a new 'i' is used.

我认为你的例子应该有效。该变量i被声明为本地变量,因此在递归时使用新的“i”。

Javascript does global and local variables!

Javascript 做全局和局部变量!

回答by brymck

I'm a little confused. iis declared locally and so it's notthe same ivariable getting modified. Tested on this very page:

我有点困惑。i是在本地声明的,因此它不是i被修改的同一个变量。在此页面上进行了测试:

var span = document.getElementsByTagName("span")[0];
function func(node) {
    for(var i = 0; i < node.children.length; i++) {
       console.log([i, node]);
       func(node.children[i]);
    } 
}
func(span);

// returns
// [0, <span id="hlinks-user">...</span>]
// [1, <span id="hlinks-user">...</span>]
// [2, <span id="hlinks-user">...</span>]
// [0, <a href="/users...">...</a>]
// [3, <span id="hlinks-user">...</span>]
// [0, <span title="1 silver...">...</span>]
// [1, <span title="1 silver...">...</span>]
// [4, <span id="hlinks-user">...</span>]
// [0, <span title="7 bronze...">...</span>]
// [1, <span title="7 bronze...">...</span>]
// [5, <span id="hlinks-user">...</span>]

回答by kchr

This worked for me.

这对我有用。

 function DragDropChanges(nodeChanged) {
        if (nodeChanged.children != null) {
            for (i = 0; i < nodeChanged.children.length;
                var temp = i;
                DragDropChanges(nodeChanged.children[i]);
                i = temp;
            }
        }
    }