Array() VS Array()


What is the fundamental difference between the two arrays when dealt with the functions pointer2array() and duplicateArray()

 

   1:  var original_arr:Array = new Array()
   2:  var some_arr:Array = new Array("one","two","three")
   3:   
   4:  function pointer2array(){
   5:      original_arr = some_arr
   6:  }
   7:  function duplicateArray(){
   8:     for(var k:Number=0;k
   9:        original_arr.push(some_arr[k]);
  10:    }
  11:  }
first using the duplicateArray()
 

   1:  trace(original_arr.toString() + newline);
   2:  duplicateArray();
   3:  some_arr.push("four")
   4:  trace("some_arr="+ some_arr.toString() + newline)
   5:  trace("original_arr="+original_arr.toString() + newline)

Now Publish the flash swf to have the below result

result:

one,two,three

some_arr=one,two,three,four

original_arr=one,two,three

Now try using the pointer2array()

      1: trace(original_arr.toString() + newline);

   2:  pointer2array();
   3:  some_arr.push("four")
   4:  trace("some_arr="+ some_arr.toString() + newline)
   5:  trace("original_arr="+original_arr.toString() + newline)
After you publish the flash swf you will be shocked to see the result
result:

one,two,three

some_arr=one,two,three,four

original_arr=one,two,three,four

The difference is that original_arr is a reference to some_arr, so be careful.

Well I am not a hard core programmer, I have been programming for days and some times you tend to forget the concepts. Just a reminder

The above code holds good for JavaScript too.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s