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

Strange Javascript results

Join Date: Jan 2007
Posts: 2,640
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 118
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Strange Javascript results

  #1  
Apr 27th, 2007
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?
Last edited by MidiMagic : Apr 27th, 2007 at 6:18 pm.
Daylight-saving time uses more gasoline
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:42 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