Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access all local variables

Every global variable in javascript is a property of 'window'

Apparently all local variable (aswell as arguments) are stored as properties of the 'Call object' (See chapter 4.6.2 of each function. Persumably only existing for the lifetime of said function.

Basically I want to be able to do

for (var obj in CallObject ) { // }

Can I access said object containing local variables and if so how? Alternatively is there any other way of accessing all local variables of a function.

[Edit]

It turns out that arguments is a property of the Activation Object of a function. This has to be stored somewhere.

"The activation object is purely a specification mechanism. It is impossible for an ECMAScript program to access the activation object."

Turns out ECMAScript states I am not allowed to access it.

What about accessing arguments properties and somehow chaining up to local variables?

like image 856
Raynos Avatar asked Sep 30 '10 15:09

Raynos


People also ask

How do I view local variables globally?

We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution operator respectively.

Where can a local variable be accessed?

Local variables can be accessed with the help of statements, inside a function in which they are declared. You can access global variables by any statement in the program. It is stored on the stack unless specified.

How will you get the information about all variables that are currently in scope?

You can see scopes and their variables in [[Scopes]] , even closure scopes using console. dir() . It shows you the variables in "[[Scopes]] > Closure", "[[Scopes]] > Global" and even "[[Scopes]] > Script" if the page have scripts reachable. Even with nested closures you can see the nested scopes.


1 Answers

No. There is no way of doing this in any browser implementation of JavaScript.

It's theoretically possible that an implementation could provide an extension to allow code to inspect the current Variable object (aka the Activation object within a function; "Call object" is a term made up by Flanagan that is not mentioned anywhere within the ECMAScript spec) but no implementation I know of does this.

like image 113
Tim Down Avatar answered Dec 13 '22 04:12

Tim Down