Connecting Tech Pros Worldwide Help | Site Map

Arrays not truly passed by reference in functions

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 2nd, 2006, 07:35 AM
Newbie
 
Join Date: Jul 2006
Posts: 4
Default Arrays not truly passed by reference in functions

:confused: This is just an absurd piece of code. Can anyone please explain the reason?

Expand|Select|Wrap|Line Numbers
  1. //a function that nulls the array passed as parameter
  2. function Nullify(arrRef)
  3. {
  4.  arrRef = null;
  5. }
  6.  
  7. var arr = new Array();
  8. for(var i = 0; i < 5; i++)
  9.   arr[arr.length] = "myString" + i;
  10.  
  11. alert("Length Before : " + arr.length); //This gives 5 as expected
  12.  
  13. Nullify(arr);  //this should null the array
  14.  
  15. alert("Length After : " + arr.length); //STRANGE : This also gives 5. Should it not give a javascript error?
According to the above code, it seems that array "arr" is not passed by reference. However if i splice( ) 2 elements in the function Nullify( ), then that alert message gives 3. ???
Reply
  #2  
Old August 2nd, 2006, 05:29 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Age: 22
Posts: 1,076
Default

I see your problem take a close look and you will to
Expand|Select|Wrap|Line Numbers
  1. function Nullify(arrRef)
  2. {
  3.  arrRef = null; // Assuming you are trying to nullify the var arr, Well you cannot do it this way you cannot use a variable as another variable get what i mean arrRef is not gonn reference to arr.
  4. }
  5.  
so this would be the way to fix it

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function Nullify()
  3. {
  4.  arr = '';
  5. }
  6.  
  7. var arr = new Array();
  8. for(var i = 0; i < 5; i++)
  9.   arr[arr.length] = "myString" + i;
  10.  
  11. alert("Length Before : " + arr.length); //This gives 5 as expected
  12.  
  13. Nullify();  //this should null the array
  14.  
  15. alert("Length After : " + arr.length); //STRANGE : This also gives 5. Should it not give a javascript error?
  16. </script>
  17.  
Reply
  #3  
Old August 3rd, 2006, 03:58 AM
Newbie
 
Join Date: Jul 2006
Posts: 4
Default

Thanks for the reply clint.

As you said

Quote:
Well you cannot do it this way you cannot use a variable as another variable get what i mean arrRef is not gonn reference to arr
But I am passing "arr" as a parameter in the function Nullify. So inside the function should not arr and arrRef point to same thing?
Instead of doing
Expand|Select|Wrap|Line Numbers
  1. arrRef = null
If i do any other changes, for instance I use splice( ) method to delete an element from arrRef.
Expand|Select|Wrap|Line Numbers
  1. arrRef.splice(0,1)
These changes are reflected pretty fine, when I come out of the function Nulllify. The alert message gives the length of array as 4 now, which is what it should be.
Only arrRef = null does not work. Apart from this any changes in arrRef are reflected in arr.

I hope i made myself clear.
Reply
  #4  
Old August 3rd, 2006, 08:47 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Age: 22
Posts: 1,076
Default

Yes and no take a look what the script you are trying does

ok so your calling the function nullify with the parameter of arr
and you got function nullify(arrRef)
so it sets arr to arrRef -- two completely separate variables

so now arrRef is an array and aar is an Array
think of it this way
arr = "a";
arrRef = arr;

now arr and arrRef both = a

down the line if i put arrRef = null;

alright so arrRef is null now but arr is still a.
Reply
  #5  
Old August 3rd, 2006, 08:52 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Age: 22
Posts: 1,076
Default

If that didn't clarify my point try this script with the alerts in there.
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //a function that nulls the array passed as parameter
  3. function Nullify(arrRef)
  4. {
  5. alert("arrRef = " + arrRef);
  6. alert("arr = " + arr);
  7. arrRef = null;
  8. alert("arrRef = " + arrRef);
  9. alert("arr = " + arr);
  10. }
  11. var arr = new Array();
  12. for(var i = 0; i < 5; i++)
  13.   arr[arr.length] = "myString" + i;
  14.  
  15. Nullify(arr);  //this should null the array
  16.  
  17.  
  18. </script>
  19.  
Reply
  #6  
Old August 4th, 2006, 06:02 AM
Newbie
 
Join Date: Jul 2006
Posts: 4
Default

Ok thanks dear. Got the point.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.