tags:

views:

455

answers:

4

Is there a way to get all variables that are currently in scope in javascript?

+1  A: 

I don't think there is. What are you trying to do?

Camsoft
I'm trying to use NetSuite's SuiteScript, and they documentation is very lacking.. I have no idea what variables are available for use when I create an event function.
Ian
+4  A: 

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. The current spec is a bit of a pain to link to (that will be fixed in the next couple of months), but if you follow this link and then grab the latest tc39-xxxx-xxx.pdf file, that's the most recent (tc39-2009-050.pdf as of this writing).

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 the arguments 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:

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 Firebug (even if you don't normally use Firefox for development). And read through whatever JavaScript files they provide you. And beat them over the head for proper docs. :-)

T.J. Crowder
+5  A: 

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:

CMS
True, but the in-scope variables go beyond just the current variable object. In-scope variables are determined by the *scope chain*, which is a chain consisting (in the normal case) of a series of variable objects and terminating with the global object (although the `with` statement can be used to insert other objects in the chain).
T.J. Crowder
+1 for links to bclary.com. A HTML version of the up-to-date spec will be appearing in the next couple of months on the ECMA website, I'm glad to say.
T.J. Crowder
@TJCrowder: The HTML version is [here](http://ecma262-5.com/), cheers!
CMS
A: 

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:

var a = 1, b = 2, c = 3;

for ( var i in window ) {
    console.log(i, typeof window[i], window[i]);
}

Which outputs, amongst 150+ other things, the following:

getInterface function getInterface()
i string i // <- there it is!
c number 3
b number 2
a number 1 // <- and another
_firebug object Object firebug=1.4.5 element=div#_firebugConsole
"Firebug command line does not support '$0'"
"Firebug command line does not support '$1'"
_FirebugCommandLine object Object
hasDuplicate boolean false

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?

Justin Johnson