Assign a value to an array passed to a function

var arrN = [1, 2, 3]; function init(arr){ arr = []; console.log(arrN) //output [1, 2, 3], expect [] } init(arrN); 

When using the splice or push methods, the array passed to the function changes. So I'm trying to figure out what happens when using the assignment operator, why doesn't it change the array? is it creating a local var passed array?

Any help would be appreciated!

+6
source share
4 answers

There is a difference in assigning another object to a variable or mutating an object that is currently referencing a variable.

(Re) appointment

When you complete a task, for example:

 arr = []; // or any other value 

... then the value that arr previously had was not changed . Just arr separated from the previous value and instead refers to the new value. The original value (if it is an object) lives on, but arr no longer has access to it.

Side note: if no other variable refers to the previous value anymore, the garbage collector will free up the memory it uses at some point in time. But this is not your case, since the global variable arrN still refers to the original value.

Mutating

Another thing is if you do not assign the value of arr , but instead apply a mutation to it, for example, with splice , push , pop or assigning one of its properties, such as arr[0] = 1 or arr[1]++ . In these cases, arr continues to refer to the same object, and changes are made to the object that it refers to, which is visible for any other variable that refers to the same object, for example arrN .

Array cleaning

Now, if you want to clear the array that is passed to your function, you should avoid doing a job like arr = ... Instead, use methods that mutate the array in place:

 arr.splice(0) 

Or alternatively:

 arr.length = 0; 

Now you have actually mutated the array, which is also the array referenced by arrN .

+2
source

You need to distinguish between a variable and an actual object (array). splice and push change the object. arr = [] just changes the variable, the old object just remains as it is.

+3
source

In JavaScript, especially when working with objects, the assignment operator ( = ) has three tasks.

  • Appointment
  • Create a new link if the right side is an object.
  • Breaking previous links and creating multiple assignments if both sides are objects.

For instance,

 var a = [1,2,3], // a is assigned an array b = a; // b is assigned just a reference to a a = ["a","b","c"]; // b to a referral is now broken // a is assigned with ["a","b","c"] // b is assigned with [1,2,3] 

the same applies if this were done in reverse order;

 var a = [1,2,3], // a is assigned an array b = a; // b is assigned just a reference to a b = ["a","b","c"]; // b to a referral is now broken // b is assigned with ["a","b","c"] // a keeps it existing assignment as [1,2,3] 
+2
source

Skip arrN to the console instead of passing arr . In addition, you simply call the function by its name, and not by the function keyword. Here is the corrected code:

 var arrN = [1, 2, 3]; function init(arr){ arr = []; console.log(arr) } init(arr); 

You also declared init() the arr argument, which is not needed in this case. When you pass the value of init() , the value of arr will be [] because you are reassigning it to functions.

+1
source

Source: https://habr.com/ru/post/1015298/