简体   繁体   English

Javascript:具有for-Loop的递归函数

[英]Javascript: Recursive function with for-Loop

This is the third question I'm asking regarding this function (should I have redone it completely at this point, maybe). 这是我要问的关于此功能的第三个问题(也许我现在应该完全重做一次)。

The function builds a grid of parameter defined size and inserts data from a function, also parameter defined. 该函数将构建一个参数定义大小的网格,并插入来自函数(同样是参数定义)的数据。 I'm trying to be able to create a grid within a grid that contains unique objects, and despite numerous problems which have been so greatly answered here and here *Thank you @Bergi, I have yet another: 我试图在包含唯一对象的网格中创建一个网格,尽管有很多问题在这里这里都得到了很好的回答*谢谢@Bergi,我还有另一个:

Here is the code, with some logs to the console to show what's up inside: 这是代码,并在控制台上显示了一些日志,以显示其中的内容:

//Build a 3x3 grid constructor that can optionally pre-insert values into each cube
function buildGrid(rows,cols,dataFunction){
    console.log("Let's see, we have an order for "+rows+" rows, "+cols+" cols and within each,"+dataFunction);
    //check to see if dataFunction provided, and type
    if(arguments.length !== 3){
        dataFunction = function(){return 0;};
    }

    var customGrid = [];

    //create grid
    for(row=0;row<rows;row++){console.log("creating row "+(row+1)+" of "+rows);
        var customRow = [];
        for(col=0;col<cols;col++){console.log("creating col "+(col+1)+" of "+cols);

            if(typeof dataFunction == 'function'){
                data = dataFunction(); 
            }

            else{
                data = dataFunction;
            }

            customRow.push(data);console.log("finished col "+(col+1)+" of "+cols);

        }
        customGrid.push(customRow);console.log("finished row "+(row+1)+" of "+rows);
    }
    console.log("Grid completed");
    return customGrid;
}

the function call looks like this: 函数调用如下所示:

var myGrid=buildGrid(3,3,function(){return buildGrid(2,2,buildSquare)});

buildSquare is just a constructor function. buildSquare只是一个构造函数。

Now here is the console log: 现在这是控制台日志:

evalresult[eval12][52]:  
Let's see, we have an order for 3 rows, 3 cols and within each, function (){return buildGrid(2,2,buildSquare)}
evalresult[eval12][61]:  
creating row 1 of 3
evalresult[eval12][63]:  
creating col 1 of 3
evalresult[eval12][52]:  
Let's see, we have an order for 2 rows, 2 cols and within each, function buildSquare(){counter++;return new Square();}
evalresult[eval12][61]:  
creating row 1 of 2
evalresult[eval12][63]:  
creating col 1 of 2
evalresult[eval12][73]:  
finished col 1 of 2
evalresult[eval12][63]:  
creating col 2 of 2
evalresult[eval12][73]:  
finished col 2 of 2
evalresult[eval12][76]:  
finished row 1 of 2
evalresult[eval12][61]:  
creating row 2 of 2
evalresult[eval12][63]:  
creating col 1 of 2
evalresult[eval12][73]:  
finished col 1 of 2
evalresult[eval12][63]:  
creating col 2 of 2
evalresult[eval12][73]:  
finished col 2 of 2
evalresult[eval12][76]:  
finished row 2 of 2
evalresult[eval12][78]:  
Grid Completed
evalresult[eval12][73]:  
finished col 3 of 3
evalresult[eval12][76]:  
finished row 3 of 3
evalresult[eval12][78]:  
Grid Completed

Spot the problem? 发现问题了吗? First the larger 3x3 function starts, and at 'data = dataFunction();', the call for the inside 2x2 function begins. 首先,较大的3x3函数开始,并在“ data = dataFunction();”处开始对内部2x2函数的调用。 Once the 2x2 function is complete, the function SHOULD jump to the next part of the 3x3 grid, but instead, returns the grid incomplete. 2x2函数完成后,该函数应跳至3x3网格的下一部分,但返回的网格不完整。 I would think that the for-loop variables (namely 'rows' and 'cols') would be tied with values within their small scope and that the inside 2x2 grid's FOR variables would not influence the FOR variables for the outer scope in the 3x3 grid. 我认为for循环变量(即“行”和“ cols”)将与它们较小范围内的值相关联,并且内部2x2网格的FOR变量不会影响3x3网格中外部范围的FOR变量。 Is this what is happening? 这是怎么回事吗?

What am I missing here? 我在这里想念什么?

Thank you all so much. 非常感谢大家。

You need to declare the variable "row" with var inside the function. 您需要在函数内部使用var声明变量“ row”。 As it stands, the variable is global, and thus when a nested call is made to "buildGrid" that code will be manipulating the same variable. 就目前而言,变量是全局变量,因此,当对“ buildGrid”进行嵌套调用时,该代码将操纵相同的变量。

Declare "data" with var too. 也用var声明“数据”。 A good habit to get into is to include the line 养成的好习惯是把

"use strict";

at the top of either your overall JavaScript block, or if that's problematic, at least at the top of your functions. 在整个JavaScript块的顶部,或者如果有问题,至少在函数的顶部。 That puts the runtime into "strict" mode, and in that mode a failure to declare a variable like that results in an error. 这将运行时置于“严格”模式,在这种模式下,无法像这样声明变量将导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM