the problem you're having is related to references/pointers vs values, which is something JS handles automagically.. ( by passing all non-primitives by reference, similar to Java )
so, when you pass an array into a function, a reference to the array is passed. if you do anything
via that array reference ( i.e. change the value of a subscript ) it will be working with the array instanced in the function caller; this is what's happening in the case of 'd' or 'z'.
however, the assignment operator in Javascript (i.e. y = z ) will not affect the referenced array, it will affect the reference itself. There's certainly two different approaches that could be implied by '='. Indeed that's why languages like C++ have values, references, pointers AND operator overloads to clear up any ambiguity.
Think of it like this, in an abstract sense, you have your array, which is made up of a 'handle', an 'array' and some values. They are connected like this:
( we don't see the italics )
handle > array > *[values]
your calling routine has a handle, when you pass the array to your function, it gets it's own handle to the same array. So in a called routine:
means; via my handle, access the nth element of the array, and set it's value to 0. Anything else with a handle to the same array will see the change; so inside, we can imagine it goes something like:
To reference:
var arrayHandle = new ArrayHandle();
arrayHandle.setArray(array);
To subscript:
//First dereference:
var tempArray = arrayHandle.dereference();
//Then subscript
tempArray[n] = 0;
//And if we're being this explicit, re-reference:
arrayHandle.setArray(tempArray);
However! if I say:
There's an ambiguity over what to assign (array, or handle?). Using the same psuedolanguage, it could go something like:
//Just re-reference the old handle to a new array:
arrayHandle.setArray(anotherArray);
In which case, your calling routine's handle would reference the new array.
It in actual fact, it will appear to do something like this:
//Create a new handle to the new array
arrayHandle = new ArrayHandle();
arrayHandle.setArray(anotherArray);
Hope that makes sense... If you want to have an array that can be altered in this way; you need to create your own 'container' class objects, that'll have methods and work in a way that is quite similar to that anecdotal 'arrayhandle' I've just talked about... in that way, you CAN control the reference to the array, because a caller and a function won't need to overwrite the reference to one of your arrayhandle objects, and the physical reference to the array in that object is then irrelevant to any level below the one you want to be working at.