5

Given:

function foo(){
  var bar = "quux";
  console.log(/*mystery code here*/);
}

I'm looking for code that when inserted into the comment would yield the value of bar. By way of illustration, something like this works in a global scope:

var foo = "bar";
var bar = "quux";
console.log(window[foo]);

But, of course, variables defined globally are appended to the window object. Variables local to a function are not. Is there some similar way to programmatically get to local function variables?

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
jemmons
  • 18,605
  • 8
  • 55
  • 84

4 Answers4

1

Nope, afraid not.

See: javascript locals()?

Community
  • 1
  • 1
Shog9
  • 156,901
  • 35
  • 231
  • 235
0

In the first place, why do you need to do that? Isn't it enough to do something like this?

console.log(bar);

Local variables are not available outside its scope in JS, but if you're trying to access them only inside your function then there's no need for an alternative way to access them.

Perhaps if you told us the real problem behind this (not just this example) we could provide you with a better alternative than looking for something like window[foo].

Seb
  • 24,920
  • 5
  • 67
  • 85
  • 1
    With respect, if it were enough I wouldn't need to ask, right? Meta-programming is afoot! Let me be explicit: function foo(name){ var bar="one"; var quux="two"; return eval(name); } foo("bar"); eval is bad. Thus I'd love to know if/where JS is stashing local variables. – jemmons Apr 13 '09 at 22:05
  • Then I'd suggest revising your implementation. There's usually a better way to request a value than passing an string and eval'ing it or fetching its value from a table - e.g. perhaps some OO approach would be better ;) – Seb Apr 13 '09 at 22:53
  • I asked a very straight forward question. If it's not possible, say so. And if you're in a position to know, an explanation as to why it's not possible is always appreciated. But instead I get replies assuming I'm an idiot telling me how I *should* be implementing my code. I understand you are just trying to help, but replies like this are not help. They are noise. – jemmons May 22 '09 at 00:42
  • 2
    I'm sorry to hear that. Plenty of times I got a better answer by asking *why* I needed to do something, while actually there were better ways. Having an open mind can help you see other answers. – Seb May 22 '09 at 01:00
-1

Very long dead thread, but won't eval do the job.

(function() {
  var foo = "bar";
  var bar = "quux";
  altert(eval(foo));
})()
Stephen Todd
  • 365
  • 3
  • 12
-1

As far as I know this is not possible and I can't see how can it be helpful. However you can find field names of any object with the for... in construct like this:

for(field in obj){
   console.debug("Obj has a field named:"+field+" with value:"+obj[field]);
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
MahdeTo
  • 11,034
  • 2
  • 27
  • 28
  • Right. While useful, I can't use this to interrogate a function for its local variables from within the function itself. Though if mistaken, please enlighten me. – jemmons Apr 13 '09 at 22:08