简体   繁体   English

javascript:修改第二个变量首先修改

[英]javascript: modifying second variable modifies first

I was trying to understand some javascript and found some quite unexpected behavior. 我试图了解一些JavaScript,发现了一些非常意外的行为。 Not knowing much about this language I wanted to find out what this behavior is called so that I can read about it formally. 我对这种语言不了解太多,所以我想找出这种行为的含义,以便我可以正式地了解它。

Here's an example of the behavior: 这是行为的示例:

var test={'x':2};

var test2=test;

test2.sourceLinks = [];

console.log('test',test);
console.log('test2',test2);

To my amazement, I found that modifying the 2nd variable somehow modifies the first as well. 令我惊讶的是,我发现修改第二个变量也会以某种方式修改第一个。 The variable "test" will also have an attribute .sourceLinks = []. 变量“ test”还将具有一个属性.sourceLinks = []。 Do I understand what's happening correctly, and if so, what's the formal term for this behavior? 我是否了解正确的情况?如果是,此行为的正式术语是什么?

I found that the answer to this is covered in How do I correctly clone a JavaScript object? 我发现如何正确克隆JavaScript对象? after I posted it, although that covered more than I was asking about. 在我发布它之后,尽管覆盖的范围超出了我的要求。

Behavior is called creating a reference . 行为称为创建参考 When variable assigned is an object actually it is not copied, rather the result of assignment is a new reference (pointer) to the object. 当分配的变量实际上是一个对象时,它不会被复制,而是分配的结果是对该对象的新引用(指针)。

This doesn't happen with primitive types: 原始类型不会发生这种情况:

  • number , 号码
  • string , 字符串
  • boolean , 布尔值
  • null , 空值
  • undefined . 未定义

But happens with all object types: 但是发生在所有对象类型上:

  • object , 对象
  • Array , 数组
  • function . 功能

This difference is significant in javascript. 这种差异在javascript中意义重大。 When some value should be passed to another scope and it might be modified there it should be passed be reference. 当某个值应该传递给另一个作用域并且可以在此处进行修改时,应该将其传递给引用。

function main(){
    var x = 1;
    modify(x);
    console.log(x);//x remains 1
}
function modify(arg){
    arg = 10;
}

Whereas when it is passed as a field of an object, it can be modified via reference to an object: 而当将其作为对象的字段传递时,可以通过引用对象来对其进行修改:

function main(){
    var o = {x : 1};
    modifyObj(o);
    console.log(o);//o.x now equals 10
}
function modifyObj(arg){
    arg.x = 10;
}

It's holding the reference . 它持有参考

When you assign object/array/function to another object/array/function it'll assign reference instead of value. 当您将对象/数组/函数分配给另一个对象/数组/函数时,它将分配引用而不是值。

To overcome this you have to clone it 为了克服这个问题,您必须克隆

When declaring a variable in Javascript you are creating an object in memory and the variable in the scope is a pointer to that memory object. 在Javascript中声明变量时,您正在内存中创建一个对象,作用域中的变量是指向该内存对象的指针。

In your example both variables (test and test2) are pointing to the same object. 在您的示例中,两个变量(test和test2)都指向同一对象。 Hence, when you modify the the either variable pointer, it is modifying the same object in memory. 因此,当您修改任一变量指针时,它就是在修改内存中的同一对象。

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

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