3 Replies Latest reply on Mar 22, 2011 7:45 PM by Ned Murphy

    Array / Function Scoping issue

    ChrisH Level 1

      Hi All,

      Can someone confirm this please?

       

      If I declare an array in a function, is that array only available / accessible in the scope of that function?  That is, does it have a local scope to the function only?

       

      I ask because I am unable to access values in the array outside of the defining function.

       

      As an example:

       

      function myFunction () {

           //other code e.g. specifying length of array, etc

       

      for (i=0; i<myArray.length; i++) {

           myArray[i] = currentValue + i;

           }

           //other code

      }


      trace (myArray); // undefined is showing up here.

       

      Do I have to do something like this inside the function to make the values available to the root and other functions?

       

      _root.myArrayGlobal = myArray; // inside the function

       

      trace (myArrayGlobal); //this then appears to work outside of the function

       

      I just want to confirm to ensure the only issue I'm having is a scoping one.

       

      If so is this the “best practice” way to do it?

       

      Thank you

      Best Regards

       

      Chris