简体   繁体   English

递归函数调用在console.log()上返回[Circular]

[英]recursive function call returns [Circular] on console.log()

So I have a script that makes an object from the current directory and everything below it. 因此,我有一个脚本,可以从当前目录及其下面的所有内容中创建一个对象。

Here's a console.log of the return statement. 这是return语句的console.log。

› node recursiveObjectifyDir.js
{ files:
   { 'LICENSE-MIT': { stat: [Object] },
     Gruntfile: { stat: [Object] } },
  dirs: { nested2: [Circular] } }

What does this [Circular] thing mean and where does it come from? 这是什么[Circular]东西的意思和它从何而来?

Here the actual script for reference: 这里以实际脚本为参考:

var fs = require('fs');
var path = require('path');

function buildFolder(folder) {
    if (!folder) {
        folder = __dirname;
    }
    nestedFiles = fs.readdirSync(folder);
    currentFile = folder + '/' + nestedFiles.pop();
    main = {
        files: {},
        dirs: {}
    };
    while (currentFile !== folder + '/undefined') {
        var fileName = path.basename(currentFile, path.extname(currentFile));
        var fileStat = fs.statSync(currentFile);
        var fullPath = folder + '/' + fileName;
        if (fileStat.isDirectory()) {
            if (fullPath.match(/git/)){
            } else if (fullPath.match(/node_modules/)){
            } else{
              var folderObj = buildFolder(fullPath);
              Object.defineProperty(main.dirs,
                  fileName, {
                      configurable: true,
                      enumerable: true,
                      value: folderObj
                  }
              );
            }
        } else {
            if (fullPath.match(/DS_Store/)){}
            Object.defineProperty(main.files,
                fileName, {
                    configurable: true,
                    enumerable: true,
                    value: {
                        stat: fileStat
                    },
                }
            );
        }
       currentFile = folder + '/' + nestedFiles.pop();
    }
    return main;
}

buildFolderReturn = buildFolder();
console.log(buildFolderReturn);

With node.js (javascript language) you have to be careful about scope. 使用node.js(JavaScript语言)时,必须注意范围。 Within your function, which is called recursively, those variables at the start you are treating as if they are in the function scope however they are accessible to your recursive calls of the same function. 在您的函数中(递归调用),这些变量在开始时就好像它们在函数范围内一样,但是您对同一函数的递归调用可以访问它们。 Just put var in front of those as you have done in the loop. 就像在循环中所做的那样,只需将var放在这些变量的前面。

var nestedFiles = fs.readdirSync(folder);
var currentFile = folder + '/' + nestedFiles.pop();
var main = {
    files: {},
    dirs: {}
};

I think that might be the problem here. 我认为这可能是这里的问题。 Put var on everything unless you expect it to be available in scope already. 将var放在所有内容上,除非您希望它在范围内已经可用。 Unless you are already in global scope but I suggest putting var on any variable which is not already in scope. 除非您已经在全局范围内,但是我建议将var放在尚不在范围内的任何变量上。

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

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