Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Arrays not truly passed by reference in functions

Question posted by: Aman Sura (Newbie) on August 2nd, 2006 07:35 AM
: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. ???
iam_clint's Avatar
iam_clint
Forum Leader
984 Posts
August 2nd, 2006
05:29 PM
#2

Re: Arrays not truly passed by reference in functions
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. }


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>

Reply
Aman Sura's Avatar
Aman Sura
Newbie
4 Posts
August 3rd, 2006
03:58 AM
#3

Re: Arrays not truly passed by reference in functions
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
iam_clint's Avatar
iam_clint
Forum Leader
984 Posts
August 3rd, 2006
08:47 PM
#4

Re: Arrays not truly passed by reference in functions
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
iam_clint's Avatar
iam_clint
Forum Leader
984 Posts
August 3rd, 2006
08:52 PM
#5

Re: Arrays not truly passed by reference in functions
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>

Reply
Aman Sura's Avatar
Aman Sura
Newbie
4 Posts
August 4th, 2006
06:02 AM
#6

Re: Arrays not truly passed by reference in functions
Ok thanks dear. Got the point.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,282 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors