Skip to content Skip to sidebar Skip to footer

Get All Variables Of The Current Function() Scope

I'm having a problem. I want to get the current function scrope. I have this example code that i'm working ok. function nittle(){ var Pen = new Dot(); // Generated dynamical t

Solution 1:

I'm not aware of any way to determine programmatically what variables have been declared inside a function, except perhaps to use nittle.toString() and then attempting to parse it yourself to find all the variables. Maybe that could work for you? (But it's too messy for me to attempt here.) UPDATE: but if the variables are created via eval() it won't work, you'd just see the eval() statement in the function's string representation.

Is there a work around ?

You could declare a single object in your function and change all the variables into properties of that object:

functionnittle() {
  var nittleVars = {
     var1 : "something",
     Pen : newDot(),
     etc : "whatever"
  };

  for (var key in nittleVars){
    if( nittleVars[key] instanceofDot ){
        alert("found it");
    }
  }
}

Your update indicates the variables are created with eval() - you could still do that with the object properties idea:

eval("nittleVars.newVar = new Dot()");  // inside the function

Post a Comment for "Get All Variables Of The Current Function() Scope"