I'm working on a simple debug function that evaluates a closure and gets all the relevant variables names in node.js. It should work at runtime. So, is there a way to automatically capture all the variable names in a closure? For example, is it possible to know the existence of "b", "c" and "d" variables in the following case?
var d=3;
function a(c){
var b = true;
eval( debug(/* need to access in runtime something like ["b","c","d"] */) );
}
function debug(vars){
var txt = "";
for(var i=0; i<vars.length; loop++){
txt+="console.log('"+vars[i]+":' + " vars[i] + ");"
}
return txt
}
I could use brute force to scan a.toString() searching for all the "var " and for all the arguments parameters, but is there a native function to find it or a special variable (something like callee.vars) to find the variables names in the closure?
I know "eval" is a big no, but I'm only using that at development environment.