简体   繁体   中英

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?

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

Shouldn't the secondList have reference to the same object to which we assigned null?

The value of list.next.next is a reference to an object.

let secondList = list.next.next; copies the reference to that object to secondList .

list.next.next = null replaces the original reference to the object with null .

It doesn't delete or modify the object itself. The value of secondList is unchanged. Since there remains a reference to the object (in secondList ), the object is not garbage collected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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