简体   繁体   English

为什么变量不清除它的值

[英]Why variable doesn't clear its value

I have next code:我有下一个代码:

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

Why doesn't the following code delete the value of the secondList variable?为什么下面的代码不删除 secondList 变量的值?

let secondList = list.next.next;
list.next.next = null;

Shouldn't the secondList have reference to the same object to which we assigned null? secondList 不应该引用我们分配给 null 的同一个 object 吗?

The value of list.next.next is a reference to an object. list.next.next 的值是对list.next.next的引用。

let secondList = list.next.next; copies the reference to that object to secondList .将对该 object 的引用复制到secondList

list.next.next = null replaces the original reference to the object with null . list.next.next = null的原始引用替换为null

It doesn't delete or modify the object itself.它不会删除或修改 object 本身。 The value of secondList is unchanged. secondList的值不变。 Since there remains a reference to the object (in secondList ), the object is not garbage collected.由于仍然存在对 object 的引用(在secondList中),因此 object 未被垃圾回收。

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

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