4

I wanted to be able to get a list of all variables in the current scope. I know it may not be possible (ex. 1, 2, 3 but it would really be helpful in simplifying a parsing algorithm for a Node/browser library I'm currently working on.

One thing: it doesn't need to be printed or safe from 'minification'.

I was wanting to be able to figure out what variables were introduced by reading a JS library and dynamically evaling it, finding the difference in state between the two. I know this approach sounds terrible on paper (I'm well acquainted with the hatred of eval), but if there is a better way to find this than just simply parsing the whole library (which is slow for any language other than C/etc.), I'm all ears.


For you all right off crying over the blatant use of eval, I know to use closures to protect the parent scope from modification. I also will be able to prevent changes to the browser display in the eval as well if it is in a browser environment (temporarily change some DOM constructors).

8
  • Sounds like you wanted to use a proper IDE…
    – Bergi
    Commented May 12, 2014 at 22:56
  • It has to do with the application I'm currently developing, a JS compiler with both Node and the browser being targets. Maybe that should give a little background. Also, I am fully aware that those kinds exist, but I'm looking to something that would be a little more fully featured in the sense of being more like GCC for JS than just a simple obfuscator. UglifyJS2 and the Closure Compiler are both seeming to poorly optimize several things IMO, and require separate tools to build and minify, which makes little sense.
    – Claudia
    Commented May 13, 2014 at 15:21
  • So you mean you're actually dealing with the syntax tree of the to-be-transpiled javascript? You would need to make that more clear, everybody assumes that you're executing code and want to programmatically access scope.
    – Bergi
    Commented May 13, 2014 at 15:25
  • Not necessarily. What everyone assumes is pretty much correct, but I'm about to post my own answer here (for what I was basically looking for).
    – Claudia
    Commented May 13, 2014 at 15:38
  • Well, if you're using eval and actually are executing (sideeffectful) code in a transpiler, then I am crying indeed.
    – Bergi
    Commented May 13, 2014 at 15:41

1 Answer 1

5

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.

4
  • 1
    Would this work: var self = this; for (var i in self) console.log(i, typeof self[i], self[i]);? I don't expect it would, but would it be possible?
    – Claudia
    Commented May 12, 2014 at 22:35
  • I don't have javascript on my computer at the moment, so I cannot verify. But it might... that'd be something to try! Commented May 12, 2014 at 22:39
  • That actually works. I'm going to post that as an answer.
    – Claudia
    Commented May 13, 2014 at 15:37
  • Actually, that is identical to the window[i] trick...
    – Claudia
    Commented May 13, 2014 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.