5

I need to do a bit of quick testing with my code (getting the value of some variables inside a function), and I want to globalise them, so I can access them through the console.

I know this method:

function foo() {
  var foo = 'foo';
  window.foo = foo; // Make foo global
}

But what if I had something like this:

function foo() {
  var foo1 = 'foo';
  var foo2 = 'foo';
  var foo3 = 'foo';
  var foo4 = 'foo';
  var foo5 = 'foo';
  var foo6 = 'foo';
  var foo7 = 'foo';
  var foo8 = 'foo';
}

What would be a quicker way to globalise all those variables, without going window.foo1 = foo1, window.foo2 = foo2, etc.?

I don't wish this to be a code golf question, just a normal programming question.

Lucas
  • 16,930
  • 31
  • 110
  • 182
  • Can you set a break point inside foo() to access the values? – beezir Jan 15 '13 at 03:16
  • @beezir Erm... don't exactly know what you're talking about... sorry... could you explain on further detail what a break point is? – Lucas Jan 15 '13 at 03:17
  • why not just make a global array and then you can construct that array with a for loop. It isn't constructed any quicker I would think (still O(N)) but at least it's a lot more accessible. – aug Jan 15 '13 at 03:17
  • `What would be a quicker way…` what is "quicker" than direct assignment? Perhaps you mean in less code. – RobG Jan 15 '13 at 03:19
  • @think123 Depending on the browser, there are different ways to do it. The absolute easiest is to add `debugger;` inside foo and open your browser's console before the code runs. In browsers like Chrome or in Firebug, you can look at the script source in the developer tools and click to the left of the line numbers to set break-points where the code will stop (just like adding the debugger line does). You can then step through the code one line at a time or check the values of local variables. – beezir Jan 15 '13 at 03:19
  • also, for the person who casted a close vote on my question, I cannot, of any means, see any similarity between the marked question and my own. – Lucas Jan 15 '13 at 03:23

3 Answers3

3

I don't think there's a way to do this. See this:

Access all local variables

Have you tried simply debugging in the console? With Chrome, you can set a breakpoint and then inspect all values. Check out this tutorial:

https://developers.google.com/chrome-developer-tools/docs/scripts-breakpoints

Community
  • 1
  • 1
Josh Rieken
  • 2,256
  • 1
  • 19
  • 23
  • yes, but that's still a deal of `console.log(foo1);`, `console.log(foo2);`, etc. – Lucas Jan 15 '13 at 03:18
  • isn't it now? I'm not really sure? – Lucas Jan 15 '13 at 03:19
  • Ah, thanks a lot. WAASAP (Will accept as soon as possible :)). – Lucas Jan 15 '13 at 03:24
  • That's pretty clever, but it only works if all the variables have the same prefix. You can't just loop over all the local variables and do something with them without somehow knowing about the names of the variables ahead of time. – Josh Rieken Jan 15 '13 at 03:37
2

Why not a single globals object instead of a bunch of variables?

function foo() {
    window.globals = {
        foo1 = 'foo',
        foo2 = 'foo',
        foo3 = 'foo',
        foo4 = 'foo',
        foo5 = 'foo',
        foo6 = 'foo',
        foo7 = 'foo',
        foo8 = 'foo'
    };
} 
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

If they're all simply named like that, you can take advantage of a little known trick, variables are actually dictionaries:

function foo() {
  var foo1 = 'foo';
  var foo2 = 'foo';
  var foo3 = 'foo';
  var foo4 = 'foo';
  var foo5 = 'foo';
  var foo6 = 'foo';
  var foo7 = 'foo';
  var foo8 = 'foo';
  for (var i = 1; i <= 8; i++) {
    window["foo" + i] = eval("foo" + i);
  }
}

document.write("Running foo...<br/>");
foo();

document.write("Printing foo...<br/>");
for (var i = 1; i <= 8; i++) {
  document.write(window["foo" + i]);
}

document.write("<br/>Just one: " + foo3);// Normal variable notation
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • `variables are actually dictionaries`. A fairly obscure comment. Variables are properties of a local variable object ([ECMA-262 ed 4](http://bclary.com/2004/11/07/#a-10.1.3)) or environment record ([ES5](http://ecma-international.org/ecma-262/5.1/#sec-10.2.1)). Your code makes use of `eval` to mimic object property access on the scope chain, but it's a dangerous trick as you can't limit the scope to the local execution context. Also, it may create properties of the window object that do not exist in the local execution context or on the scope chain. – RobG Jan 15 '13 at 04:05
  • I appreciate the clarification. My over simplified statement was all I could come up with off the top of my head. As for the eval, I'm looking into ways to avoid that, I really do hate using it. – Corey Ogburn Jan 15 '13 at 15:26