0
var test = function () 
{   

   var my_array =[1,2,3];

   my_function = function  (my_array) 
   {
    my_array = global_fun().slice();
    console.log(my_array.length); // >> 10 !!!!!!
   }

   my_function (my_array);
   console.log(my_array.length); // >> 3 !!!!!!
   // my_array keeps being 1,2,3   

}

var global_fun = function ()
{    
var array =[];
array=[9,8,7,6,5,4,3,2,1,0];
return array;
}

Please, copy this code and run test(); You can see how my_array keeps being the same .

Why this does not work ? I dont want to push values, I want a complete array change.

I feel like a st....

civiltomain
  • 1,136
  • 1
  • 9
  • 27
  • Change my_function=function(my_array) to my_function=function(my_array_2) – El Guapo Dec 16 '14 at 17:41
  • `my_array` is a parameter of and therefore local to your `my_function` function. Assigning it will not change the `my_array` variable of `test`… – Bergi Dec 16 '14 at 17:48

2 Answers2

1

You're getting a mess because you use the same variable name in different scopes. Have a look at a few issues:

  • my_array = global_fun().slice(); this only updates the my_array inside the my_function
  • my_function doesn't return it, so it's lost forever

See the modified version here, I've changed my_function so it returns the modified array and I assign the returned value to the my_array in the test function scope:

var test = function() {

  var my_array = [1, 2, 3];            // 1st one created/declared

  my_function = function(my_array) {   // 2nd one created as a parameter
    my_array = global_fun().slice();   // 2nd one modified, NOT the 1st one
    console.log(my_array.length); // >> 10 !!!!!!
    return my_array;                   // returning 2nd one
  }

  my_array = my_function(my_array);    // 1st one gets the value of the 2nd one
  console.log(my_array.length); // >> no longer 3 !!!!!!
  document.body.innerHTML = 'End array: ' + my_array;
}

var global_fun = function() {
  var array = [];
  array = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
  return array;
}

test();
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • The problem is that I dont want to return values .... mmm , I'm going to write a replace function... Th. anyway – civiltomain Dec 16 '14 at 18:07
  • Yup, you can always modify the contents of the array, but not replace the whole thing at once. – Shomz Dec 16 '14 at 18:11
-1

The idea is that you expect value is passed by reference while it is value based

check this SO question Does Javascript pass by reference?

you have to return the updated value after updating it as follows:

my_function = function  (my_array) 
   {
    my_array = global_fun().slice();
    console.log(my_array.length); // >> 10 !!!!!!
    return my_array; 
   }

Then call it as follows:

var updatedArray = my_function (my_array);
   console.log(updatedArray .length);
Community
  • 1
  • 1
Muhammad Gouda
  • 849
  • 8
  • 20
  • Yes, what did you write that I haven't already written (and explained) in my answer 10 minutes before you? – Shomz Dec 16 '14 at 21:29
  • @Shomz, thanks for clarification. Actually, I started writing my answer before you submit yours, and submitted it before reading yours. I am not sure that the gap was 10 minutes. Also, I got that the asker needs to understand why his approach did not work, not just get his task done. and my answer includes an explanation why it didn't work. While your answer provided a working approach without explaining what's wrong with current one – Muhammad Gouda Dec 16 '14 at 21:37