0
var so:SharedObject = SharedObject.getLocal("example");
var arr:Array=[1,2,3];
so.data.arr = arr;
so.flush();
trace(so.data.arr); // outputs 1,2,3
arr[2]=5;
trace(so.data.arr); // outputs 1,2,5

As you see, I updated only arr, but so.data.arr got updated as well.

so.data.arr doesn't update if instead of arr[2]=5; i write arr=[1,2,5];

Seems that arr and so.data.arr are linked somehow, but only if I update an element in the arr, not set the whole arr differently.

I discovered this accidentally. Why is it working like that?

Can I count that it works like that every time and use it? Thanks.

DBC
  • 69
  • 7

1 Answers1

2

Basically speaking arrays are passed by reference, not by value. That means if you assign an array variable to another one you are not creating a new array.

so.data.arr = arr;

means that both so.data.arr and arr point to the same array object. That's why modifications to either one will be reflected by the other. They are pointing at the same thing. But

arr=[1,2,5];

will make arr point to some other array object, remember that [1,2,5] is a short hand version of new Array(1,2,5).

That's why after that line they aren't "linked" any more.

null
  • 5,207
  • 1
  • 19
  • 35