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)
<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?