•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 392,088 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,938 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1142 | Replies: 6
![]() |
I want to understand the difference in parameter passing between the three methods used:
- Returning an array in the return statement. (variable a in the code below)
- Returning an array copied as a unit from another array. (variable c in the code below)
- Returning an array, each element individually set. (variable d in the code below)
In both IE and Firefox, the alert in the function returns:
But the alert in the calling program returns:
Why is the variable c undefined?
- Returning an array in the return statement. (variable a in the code below)
- Returning an array copied as a unit from another array. (variable c in the code below)
- Returning an array, each element individually set. (variable d in the code below)
<html>
<head>
<title>A test</title>
<script type="text/javascript">
function tstarray(x,y,z){
var i;
var w = Array(4);
for(i=0;i<4;i++) {
w[i] = x[i];
z[i] = x[i];
};
y = z;
alert('function ' + w + ', ' + x + ', ' + y + ', ' + z);
return w;
}
</script>
</head>
<body>
<script type="text/javascript">
var b = [1,2,3,4];
var a = Array(4);
var c = Array(4);
var d = Array(4);
a = tstarray(b,c,d);
alert('return ' + a + ', ' + b + ', ' + c + ', ' + d);
</script>
</body>
</html>In both IE and Firefox, the alert in the function returns:
function 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4
But the alert in the calling program returns:
function 1,2,3,4, 1,2,3,4, ,,,, 1,2,3,4
Why is the variable c undefined?
Last edited by MidiMagic : Apr 27th, 2007 at 5:18 pm.
Daylight-saving time uses more gasoline
There is an error in my post. The calling program actually returns:
return 1,2,3,4, 1,2,3,4, ,,,, 1,2,3,4
Last edited by MidiMagic : Apr 27th, 2007 at 10:49 pm.
Daylight-saving time uses more gasoline
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation:
Rep Power: 5
Solved Threads: 47
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:
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:
However! if I say:
There's an ambiguity over what to assign (array, or handle?). Using the same psuedolanguage, it could go something like:
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:
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.
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]
array[n] = 0
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);
array = anotherArray
//Just re-reference the old handle to a new array: arrayHandle.setArray(anotherArray);
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 12:42 am.
If it only works in Internet Explorer; it doesn't work.
Thanks. That makes sense.
It also explains how altering the contents of the copy array in the function changed the original array (which I wanted untouched) in the callong program.
So how do I make a real copy of the array that I can change without affecting the original? Do I need to loop through all of the elements and copy them individually?
It also explains how altering the contents of the copy array in the function changed the original array (which I wanted untouched) in the callong program.
So how do I make a real copy of the array that I can change without affecting the original? Do I need to loop through all of the elements and copy them individually?
Daylight-saving time uses more gasoline
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation:
Rep Power: 5
Solved Threads: 47
•
•
•
•
So how do I make a real copy of the array that I can change without affecting the original? Do I need to loop through all of the elements and copy them individually?
Pretty much; unless Javascript has an Array.copy() function or similar... inbuilt functions might be better optimized, but I doubt it'll make much difference in this case.
.. well, according to this page, one can hijack the Array.slice() method ( which is supposed to return a copy of a segment of an array ) to copy the whole Array.. I really don't know if that would be any more efficient than manual copying, but maybe it's worth a benchmark if you need the speed:
http://www.sematopia.com/?p=12
( Read the comments; the original poster seems to have ommited a neccessary parameter, and someone there explains what happens if you copy arrays of arrays or other arrays of references with .slice(); if you do so, you might want to do it manually, just to retain control )
If it only works in Internet Explorer; it doesn't work.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Hai All, Can anybody solve this problem its urgent
- Next Thread: xhtml-javascript taxtarea problem



Linear Mode