0

I am working on google maps and I need to create an array of items. Here is my pseudo code:

<script>
var myvar=new array();

function initialize(){
   for i=1 to 10
   {  
        callAnotherFunct(i);
   }

   access myvar() here; 
}


function callAnotherFunct(i){
    myvar=array_element_i;
}

</script>

I am expecting myvar to behave as a global variable but it is not. I cannot get any values for myvar in the initialize().

What am I doing wrong here?

7
  • 1
    Can we see the actual code, please? It may help spot the problem Commented Jul 6, 2011 at 18:30
  • 2
    is this pseudocode? bc if this is your real code it wont do anything, it is completely wrong Commented Jul 6, 2011 at 18:31
  • How are you accessing the values? Commented Jul 6, 2011 at 18:31
  • Why not post real code? What's the thinking behind providing us with a mangled version of something that you must surely have tried in real JavaScript? Commented Jul 6, 2011 at 18:32
  • var myGlobalVar = {} and then use something like myGlobalVar.myArray = new Array() Probably a namespace collision. I had the same issue and using a separate namespace seemed to work :) Commented Jul 6, 2011 at 18:35

6 Answers 6

2

pseudo-schmeudo.

var myvar = [];

function initialize(){
   for (var i=0; i < 10; i++)
   {  
        callAnotherFunct(i);
   }

   alert(myvar[0]);
   alert(myvar[9]);
}


function callAnotherFunct(i){
    myvar[i]=i + 'pseudo-schmeudo';
}

initialize();

Fiddle-schmiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure what you were trying to accomplish, but I was able to make several modifications and was able to access the global variable in this example: http://jsfiddle.net/pKU6A/

var myvar=new Array(); //Array should be uppercase

function initialize(){
   for (var i=1; i < 10; i++) //incorrect for loop syntax
   {  
        callAnotherFunct(i);
   }

  alert(myvar);
}


function callAnotherFunct(i){
    myvar[i] = i; //local variable was not defined and index of array must be assigned
}

initialize(); //needed to call global function to kick it off

Comments

1

fiddle: http://jsfiddle.net/AKKHB/

Seems to be ok

Comments

1

It is hard to tell what you might be doing wrong - with the pseudocode.

I have de-pseudified your code and it works fine:

var myvar=new Array();

function initialize(){
  for (i=1; i < 10; i++)
  {  
    callAnotherFunct(i);
  }
  alert(myvar);
  //access myvar() here; 
}

function callAnotherFunct(i){
  myvar.push(i);
}

when you call initialize() - it will alert with 1,2,3,4,5,6,7,8,9

Hope that helps

Comments

0
window.myvar = []; // don't use new Array()

function initialize(){
   for i=1 to 10
   {  
        callAnotherFunct(i);
   }

   //window.myvar or myvar here should work
}

Comments

0

I am guessing this is a namespace issue. Do something like this

window.project = window.project || {};
project.vars = project.vars || {};

Then you will have a namespace declaration, so you can do

project.vars.myVar = new Array();

That's the only issue I could think of

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.