简体   繁体   English

为传递给函数的数组赋值

[英]Assign value to the 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 splice or push methods the array passed to a function is being modified. 使用splicepush方法时,正在修改传递给函数的数组。 So I am trying to understand what is happening when using assignment operator, why it's not changing the array? 所以我试图理解使用赋值运算符时发生了什么,为什么它不改变数组呢? is it creating the local var of the passed array? 是创建传递数组的局部var

Any help will be appreciated! 任何帮助将不胜感激!

You need to distinguish between the variable and the actual object (the array). 您需要区分变量和实际对象(数组)。 splice and push are changing the object. splicepush正在改变对象。 arr = [] is just changing the variable, the old object just stays as it is. arr = []只是更改变量,旧对象保持原样。

There is a difference in assigning a different object to a variable or mutating the object currently referenced by a variable. 将不同的对象分配给变量或改变当前由变量引用的对象是有区别的。

(Re)assigning (Re)的分配

When you do an assignment like: 当您执行如下任务时:

arr = []; // or any other value

... then the value that arr previously had is not altered . ......然后该值arr以前有没有改变 It is just that arr detaches from that previous value and references a new value instead. 只是arr与之前的值分离并引用了一个新值。 The original value (if it is an object) lives on, but arr no longer has access to it. 原始值(如果它是一个对象)继续存在,但是arr不再能够访问它。

Side note: if no other variable references the previous value any more, the garbage collector will at some point in time free the memory used by it. 旁注:如果没有其他变量再引用前一个值,垃圾收集器将在某个时间点释放它使用的内存。 But this is not your case, since the global variable arrN still references the original value. 但这不是你的情况,因为全局变量arrN仍然引用原始值。

Mutating 变异

It is another thing if you don't assign a value to arr , but apply instead a mutation to it, for instance with splice , push , pop , or an assignment to one of its properties, like arr[0] = 1 or arr[1]++ . 这是另一回事,如果你不分配一个值arr ,而是突变适用于它,例如与splicepushpop ,或分配给它的一个属性,像arr[0] = 1arr[1]++ In those cases, arr keeps referencing the same object, and the changes are made to the object it references, which is visible to any other variable that references the same object, like arrN . 在这种情况下, arr保持引用同一个对象,并更改它所引用的对象,这引用同一个对象,就像任何其他变量可见制作arrN

Clearing an array 清除阵列

Now if you want to clear the array that is passed to your function, you must avoid to make an assignment like arr = ... . 现在,如果要清除传递给函数的数组,则必须避免进行类似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 . 现在你实际上已经改变了数组,这也是arrN引用的数组。

In JavaScript, particularly when working with objects the assignment operator ( = ) has three jobs. 在JavaScript中,特别是在处理对象时,赋值运算符( = )有三个作业。

  1. Assignment 分配
  2. Creating a new reference if the right side is an object. 如果右侧是对象,则创建新引用。
  3. Breaking any previous referencing and creating multiple assignments if both sides are objects. 如果双方都是对象,则中断任何先前的引用并创建多个赋值。

Such as; 如;

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]

same would apply if it was 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's existing assignment as [1,2,3]

Your are passing arrN to the console instead of passing arr . 你正在将arrN传递给控制台而不是传递arr Also, you just call a function by its name, not by the keyword function . 此外,您只需按名称调用函数,而不是通过关键字function Here is the corrected code: 这是更正后的代码:

var arrN = [1, 2, 3];

function init(arr){
   arr = [];
   console.log(arr) 
}

init(arr);

You have also declared init() with an argument arr , which is not needed in this case. 您还使用参数arr声明了init() ,在这种情况下不需要。 What ever the value you pass to init() , the value of arr will be [] because you are reassigning it in the function. 无论你传递给init()的值是什么, arr的值都是[]因为你在函数中重新分配它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM