RSS Forums RSS
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting

Strange Javascript results

Join Date: Jul 2006
Location: Deptford, London
Posts: 985
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 6
Solved Threads: 51
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Strange Javascript results

  #4  
Apr 29th, 2007
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:
array[n] = 0
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:
array = anotherArray
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.
Last edited by MattEvans : Apr 29th, 2007 at 1:42 am.
Plato forgot the nullahedron..
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:05 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC