0

I need to pass an object to another function but i have problems with global variables being referenced inside the object.

var a = 'first variable'; //SAMPLE GLOBAL VARIABLE
var b = 'second variable'; //SAMPLE GLOBAL VARIABLE

function iterator(options){
   for(var c in options){
    alert(c + ' ' + options[c]);    
   }
}

function main(){
   iterator({
      a:'5',
      b:'6'
   });
}

The functions should alert the output below

first variable 5
second variable 6

But instead I'm getting

a 5
b 6

I need to use variables inside the object.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kiel
  • 483
  • 3
  • 5
  • 18
  • Don't use local variable the same name as the global's it looks like you looking for troubles, there are tons of variables names that you can use. – lastboy Jan 13 '14 at 07:05
  • yup updated but still doesn't work – Kiel Jan 13 '14 at 07:10
  • @Kiel check my answer now. `alert(a + ' ' + options[c]); ` – Linga Jan 13 '14 at 07:12
  • possible duplicate of [Javascript: creating object with dynamic keys](http://stackoverflow.com/questions/19837916/javascript-creating-object-with-dynamic-keys) – Felix Kling Jan 13 '14 at 07:35

5 Answers5

2

The a in the function is the local variable declared in the for loop.

var a = 'first variable'; //SAMPLE GLOBAL VARIABLE
var b = 'second variable'; //SAMPLE GLOBAL VARIABLE
var opts = {a: a, b: b};

function iterator(options){
   for(var a in options){
    alert(opts.a + ' ' + options[a]);    
   }
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • sorry but the global variable opts cannot be global because i would be passing the object to a javascript library. Sorry if i did not mention i thought it wasn't a factor. – Kiel Jan 13 '14 at 07:07
  • Didn't work sorry. The two functions are actually separated to 2 javascript files. There will be lots of changes in the code to access the global variable opts from the other javascript file. But if I only have 1 file this works. – Kiel Jan 13 '14 at 07:13
  • @ling.s sorry just look at my comment to your answer and thank you – Kiel Jan 13 '14 at 07:23
2

As per your line I need to use variables inside the object, I suggest you rewrite your main as below:

function main(){
   var obj = {}
   obj[a] = '5';
   obj[b] = '6';   
   iterator(obj);
}

Since, a inside for-in loop will create new variable inside loop and refer to each key of object

Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
  • This one works thank you. I might need to change alot of codes lol – Kiel Jan 13 '14 at 07:21
  • Also for the question above here is the answer for the single js http://jsfiddle.net/nB5m3/2/ from @ling.s – Kiel Jan 13 '14 at 07:29
  • @Kiel: This stackoverflow wouldn't be raised if you would have used different variable name inside for-in loop :). Since, you would have got what you wanted there itself. :) Anyway, Cheers!!! – Dhanu Gurung Jan 13 '14 at 07:33
1

The a & b which you trying to access are tied to the Window object.

When you were printing 'a' or 'b' otherwise, it was printing the key 'a' from the Key/Value of you JSON.

$(window).load(function() {
main();
});

and,

function iterator(options){
   for(var a in options){
    alert(window[a] + ' ' + options[a]);    
   }
}

Fiddle

Linga
  • 10,379
  • 10
  • 52
  • 104
gauravmuk
  • 1,606
  • 14
  • 20
1

According to the edit, the OP now changed the variable name, so now variable a can be directly accessed

function iterator(options){
 for(var c in options){
     alert(a + ' ' + options[c]);    
 }
}

Here is the Fiddle

Linga
  • 10,379
  • 10
  • 52
  • 104
  • this works with one js file. I need the main function in a separate file that is why i cannot access the global variable a. I just made the js here simplier but the js file where the `function iterator` is present is actually inside a js library that i would access from the main javascript. – Kiel Jan 13 '14 at 07:17
  • thanks and sorry if i didn't mention the js library. – Kiel Jan 13 '14 at 07:22
0

The var a here is specifically for creating a new variable with function scope that shadows the global variable:

function iterator(options){
   for(var a in options){
    alert(a + ' ' + options[a]);    
   }
}

You can't do that, if you still want to access the global...

user229044
  • 232,980
  • 40
  • 330
  • 338