Getting All Variables In Scope
Is there a way to get all variables that are currently in scope in javascript?
Is there a way to get all variables that are currently in scope in javascript?
Although everyone answer "No" and I know that "No" is the right answer but if you really need to get local variables of a function there is a restricted way.
Consider this function:
var f = function() {
var x = 0;
console.log(x);
};
You can convert your function to a string:
var s = f + '';
You will get source of function as a string
'function () {\nvar x = 0;\nconsole.log(x);\n}'
Now you can use a parser like esprima to parse function code and find local variable declarations.
var s = 'function () {\nvar x = 0;\nconsole.log(x);\n}';
s = s.slice(12); // to remove "function () "
var esprima = require('esprima');
var result = esprima.parse(s);
and find objects with:
obj.type == "VariableDeclaration"
in the result (I have removed below):console.log(x)
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "x"
},
"init": {
"type": "Literal",
"value": 0,
"raw": "0"
}
}
],
"kind": "var"
}
]
}
I have tested this in Chrome, Firefox and Node.
But the problem with this method is that you just have the variables defined in the function itself. For example for this one:
var g = function() {
var y = 0;
var f = function() {
var x = 0;
console.log(x);
};
}
you just have access to the x and not y. But still you can use chains of caller (arguments.callee.caller.caller.caller) in a loop to find local variables of caller functions. If you have all local variable names so you have scope variables. With the variable names you have access to values with a simple eval.
No. "In scope" variables are determined by the "scope chain", which is not accessible programmatically.
For detail (quite a lot of it), check out the ECMAScript (JavaScript) specification. Here's a link to the official page where you can download the canonical spec (a PDF), and here's one to the official, linkable HTML version.
Update based on your comment to Camsoft
The variables in scope for your event function are determined by where you define your event function, not how they call it. But, you may find useful information about what's available to your function via and arguments by doing something along the lines of what KennyTM pointed out () since that will tell you what's available on various objects provided to you ( and arguments; if you're not sure what arguments they give you, you can find out via the variable that's implicitly defined for every function).this
for (var propName in ____)
this
arguments
So in addition to whatever's in-scope because of where you define your function, you can find out what else is available by other means by doing:
var n, arg, name;
alert("typeof this = " + typeof this);
for (name in this) {
alert("this[" + name + "]=" + this[name]);
}
for (n = 0; n < arguments.length; ++n) {
arg = arguments[n];
alert("typeof arguments[" + n + "] = " + typeof arg);
for (name in arg) {
alert("arguments[" + n + "][" + name + "]=" + arg[name]);
}
}
(You can expand on that to get more useful information.)
Instead of that, though, I'd probably use a debugger like Chrome's dev tools (even if you don't normally use Chrome for development) or Firebug (even if you don't normally use Firefox for development), or Dragonfly on Opera, or "F12 Developer Tools" on IE. And read through whatever JavaScript files they provide you. And beat them over the head for proper docs. :-)