Is there a way to get all variables that are currently in scope in javascript?
标签:
javascript
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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
this
and arguments by doing something along the lines of what KennyTM pointed out (for (var propName in ____)
) since that will tell you what's available on various objects provided to you (this
and arguments; if you're not sure what arguments they give you, you can find out via thearguments
variable that's implicitly defined for every function).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:
(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. :-)
The Simplest Way to Get Access to Vars in a Particular Scope
Note: You want to do this against un-minified js.
The Simplest Way to Show All Non-Private Vars
Now you will see an object tree you can expand with all declared objects.
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:
You can convert your function to a string:
You will get source of function as a string
Now you can use a parser like esprima to parse function code and find local variable declarations.
and find objects with:
in the result (I have removed
console.log(x)
below):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:
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.
get a list of each and every word ever having been written or typed in any way or fashion by any human since the beginning of time (definitely a finite list in the mathematical sense.)
put them all into one big array by typing them all again into your dev console (as strings, so you don't get error thrown here.)
create new array, and do a loop over your finite list, pushing into the new array if a try / catch (doing just that in the try) does not end in the catch, due to ReferenceError (using eval to "unstring", because you do want the error here, if not in scope).
I take that back. The list I was talking about in point 1 is not big enough. They could have created variables via window[randomlyGeneratedString] = something. Better loop over all strings which can be generated by any computer program halting in reasonable feasible time frame - say, the time since the invention of the computor.
OK, seriously, you could do it like that, using as the list in 1) what you get from running esprima.parse on your entire codebase and walking the tree for things called "Identifier" and storing their "name". But then you will miss variables being created as in window["file_" + i] = blah, see 4.
As everyone noticed: you can't. But you can create a obj and assign every var you declare to that obj. That way you can easily check out your vars:
Yes and no. "No" in almost every situation. "Yes," but only in a limited manner, if you want to check the global scope. Take the following example:
Which outputs, amongst 150+ other things, the following:
So it is possible to list some variables in the current scope, but it is not reliable, succinct, efficient, or easily accessible.
A better question is why do you want to know what variables are in scope?