简体   繁体   English

javascript:从局部变量修改全局变量

[英]javascript: modify global variable from local variable

I don't understand why in the function change2 , the console doesn't output 2, but undefined? 我不明白为什么在功能change2 ,控制台不输出2,但是未定义?

var users = {a:1, b:2};

function change1(people){
    people.c = {};
    var C = people.c;
    setTimeout(function(){        
        C.number = 2;
    }, 1000);
}

function change2(people){
    people.c = {};
    var C = people.c;
    setTimeout(function(){
        console.log(C.number);
    }, 2000);
}

change1(users);
change2(users); // undefined, why not 2?

However, if I replace C.number by people.c.number within setTimeout of change1 , it works(outputs 2), why? 不过,如果我取代C.number通过people.c.numbersetTimeoutchange1 ,它的工作原理(输出2),为什么呢? Don't they refer the same thing? 他们不是指同一件事吗?

Your change1 function assigns a new object to people.c and also stores it in local C variable. 您的change1函数将一个新对象分配给people.c并将其存储在本地C变量中。 Then in change2 you create another new object and store is in people.c. 然后在change2中创建另一个新对象,并将其存储在people.c中。 So people.c (or rather users.c) holds now that second object. 因此,people.c(或更确切地说是users.c)现在拥有第二个对象。

After 1 second your change1 function creates a "number" property in the first object. 1秒钟后,您的change1函数将在第一个对象中创建一个“数字”属性。 But people.c already holds a second object which stays not modified. 但是people.c已经拥有第二个对象,该对象保持不变。 So after another 1 second change2 tries to find "number" property in the second object which do not have such property. 因此,在另一秒钟后,change2尝试在第二个不具有此属性的对象中查找“ number”属性。

So in short you should remove 总之,您应该删除

people.c = {};

from change2 so avoid overriding the people.c property. 从change2开始,因此请避免覆盖people.c属性。

This is my explanation. 这是我的解释。 In the change1 this creates a new object, lets call it object1, and keeps a reference in the local variable C: 在change1中,这将创建一个新对象,将其称为object1,并将引用保留在局部变量C中:

  people.c = {};
  var C = people.c;

In change2 let change the name of C in B (for avoiding confusion) 在change2中,更改B中C的名称(为避免混淆)

people.c = {};
var B = people.c;

This creates another new object, lets call it object2, that overrides the reference in people.c (so people.c points to this new object) and stores a reference to the local variable B. 这将创建另一个新对象,我们称其为object2,它将覆盖people.c中的引用(因此people.c指向此新对象)并存储对局部变量B的引用。

So the local variable C in change1 points to object1, the local variable B points to object2 and people.c points to object2. 因此,change1中的局部变量C指向对象1,局部变量B指向对象2,people.c指向对象2。

C.number = 2

will define the number property of object1 in change1 将在change1中定义object1的number属性

meanwhile 与此同时

console.log(B.number) //console.log(C.number) in the original code

will try to output the property of object2 that is undefined. 将尝试输出未定义的object2的属性。

Basically C in change1 and C in change2 are pointing to two different objects. 基本上,change1中的C和change2中的C指向两个不同的对象。 setTimeout does not have any effect in this. setTimeout对此没有任何影响。

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

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