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


so have script makes object current directory , below it.

here's console.log of return statement.

› node recursiveobjectifydir.js { files:    { 'license-mit': { stat: [object] },      gruntfile: { stat: [object] } },   dirs: { nested2: [circular] } } 

what [circular] thing mean , come from?

here actual script 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) have careful scope. within function, called recursively, variables @ start treating if in function scope accessible recursive calls of same function. put var in front of have done in loop.

var nestedfiles = fs.readdirsync(folder); var currentfile = folder + '/' + nestedfiles.pop(); var main = {     files: {},     dirs: {} }; 

i think might problem here. put var on unless expect available in scope already. unless in global scope suggest putting var on variable not in scope.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -