<p>In Python one can get a dictionary of all local and global variables in the current scope with the built-in functions <code>locals()</code> and <code>globals()</code>.  Is there some equivalent way of doing this in Javascript?  For instance, I would like to do something like the following:</p>

<pre><code>var foo = function(){ alert('foo'); };
var bar = function(){ alert('bar'); };

var s = 'foo';
locals()[s](); // alerts 'foo'
</code></pre>

<p>Is this at all possible, or should I just be using a local object for the lookup?</p>

[{'answer_id': 39982, 'author': 'Bartosz Bierkowski', 'author_id': 3666, 'author_profile': 'https://Stackoverflow.com/users/3666', 'pm_score': 2, 'selected': False, 'text': '<p>Well, I don\'t think that there is something like that in js. You can always use eval instead of locals(). Like this: </p>\n\n<pre><code>eval(s+"()");\n</code></pre>\n\n<p>You just have to know that actually function <em>foo</em> exists.</p>\n\n<p><strong>Edit:</strong></p>\n\n<p>Don\'t use eval:) Use:</p>\n\n<pre><code>var functionName="myFunctionName";\nwindow[functionName]();\n</code></pre>\n'}, {'answer_id': 39992, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': -1, 'selected': False, 'text': '<p>AFAIK, no. If you just want to check the existence of a given variable, you can do it by testing for it, something like this:</p>\n\n<pre><code>if (foo) foo();\n</code></pre>\n'}, {'answer_id': 40012, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 2, 'selected': False, 'text': '<p>I seem to remember Brendan Eich commented on this in a recent podcast; if i recall correctly, it\'s not being considered, as it adds unreasonable restrictions to optimization. He compared it to the <code>arguments</code> local in that, while useful for varargs, its very existence removes the ability to guess at what a function will touch just by looking at its definition. </p>\n\n<p>BTW: i believe JS <em>did</em> have support for accessing locals through the arguments local at one time - <a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Functions/arguments" rel="nofollow noreferrer">a quick search</a> shows this has been deprecated though.</p>\n'}, {'answer_id': 40035, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 0, 'selected': False, 'text': "<p>@e-bartek, I think that window[functionName] won't work if you in some closure, and the function name is local to that closure.  For example:</p>\n\n<pre><code>function foo() {\n    var bar = function () {\n        alert('hello world');\n    };\n    var s = 'bar';\n    window[s](); // this won't work\n}\n</code></pre>\n\n<p>In this case, s is 'bar', but the function 'bar' only exists inside the scope of the function 'foo'.  It is not defined in the window scope.</p>\n\n<p>Of course, this doesn't really answer the original question, I just wanted to chime in on this response.  I don't believe there is a way to do what the original question asked.</p>\n"}, {'answer_id': 40067, 'author': 'Bartosz Bierkowski', 'author_id': 3666, 'author_profile': 'https://Stackoverflow.com/users/3666', 'pm_score': 0, 'selected': False, 'text': '<p>@pkaeding</p>\n\n<p>Yes, you\'re right. <em>window[functionName]()</em> doesn\'t work in this case, but <em>eval</em> does. If I needed something like this, I\'d create my own object to keep those functions together.</p>\n\n<pre><code>var func = {};\nfunc.bar = ...;\nvar s = "bar";\nfunc[s]();\n</code></pre>\n'}, {'answer_id': 40173, 'author': 'sverrejoh', 'author_id': 473, 'author_profile': 'https://Stackoverflow.com/users/473', 'pm_score': 5, 'selected': True, 'text': '<ul>\n<li><p>locals() - No. </p></li>\n<li><p>globals() - Yes.</p></li>\n</ul>\n\n<p><code>window</code> is a reference to the global scope, like <code>globals()</code> in python.</p>\n\n<pre><code>globals()["foo"]\n</code></pre>\n\n<p>is the same as:</p>\n\n<pre><code>window["foo"]\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/39960', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/208/']