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?
You can't.
Variables, identifiers of function declarations and arguments for function code, are bound as properties of the Variable Object, which is not accesible.
See also:
If you just want to inspect the variables manually to help debug, just fire up the debugger:
debugger;
Straight into the browser console.
In ECMAScript 6 it's more or less possible by wrapping the code inside a
with
statement with a proxy object. Note it requires non-strict mode and it's bad practice.The proxy claims to own all identifiers referenced inside
with
, so variable assignments are stored in the target. For lookups, the proxy retrieves the value from the proxy target or the global object (not the parent scope).let
andconst
variables are not included.Inspired by this answer by Bergi.