gpt4 book ai didi

javascript - 当该成员从数组中删除时,我们如何访问该成员?

转载 作者:行者123 更新时间:2023-11-28 11:14:52 26 4
gpt4 key购买 nike

var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];

var member = arr[0];

arr.splice(0, 1);

console.log(member);

最佳答案

您可以继续访问它,因为您仍然拥有对它的引用。它不再存在于数组中,但仍然存在。它将继续存在,直到对它的所有引用都被删除,当它们被删除时,它就符合垃圾回收的条件。

在此之后:

var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];

你的内存中有这样的东西(省略了一些细节):

                  +−−−−−−−−−−−−+arr:Ref4875−−−−−−>|   (array)  |                  +−−−−−−−−−−−−+      +−−−−−−−−−−−−−−+                  | 0: Ref8612 |−−−−−>|   (object)   |                  | 1: Ref4631 |−−+   +−−−−−−−−−−−−−−+                  +−−−−−−−−−−−−+  |   | id: 1        |                                  |   | name: "John" |                                  |   +−−−−−−−−−−−−−−+                                  |                                  |   +−−−−−−−−−−−−−−+                                  +−−>|   (object)   |                                      +−−−−−−−−−−−−−−+                                      | id: 1        |                                      | name: "John" |                                      +−−−−−−−−−−−−−−+

(The "Ref1234" values are called object references. You never see their actual values, but you can think of them as a number the JavaScript engine uses to look up where the object is in memory.)

Then after this:

var member = arr[0];

member 和数组都引用该对象,如下所示:

member:Ref8612−−−−−−−−−−−−−−−−−−−−−+                  +−−−−−−−−−−−−+   |arr:Ref4875−−−−−−>|   (array)  |   |                  +−−−−−−−−−−−−+   |  +−−−−−−−−−−−−−−+                  | 0: Ref8612 |−−−+−>|   (object)   |                  | 1: Ref4631 |−−+   +−−−−−−−−−−−−−−+                  +−−−−−−−−−−−−+  |   | id: 1        |                                  |   | name: "John" |                                  |   +−−−−−−−−−−−−−−+                                  |                                  |   +−−−−−−−−−−−−−−+                                  +−−>|   (object)   |                                      +−−−−−−−−−−−−−−+                                      | id: 1        |                                      | name: "John" |                                      +−−−−−−−−−−−−−−+

Then after:

arr.splice(0, 1);

数组不再有对它的引用,但 member 仍然有:

                                      +−−−−−−−−−−−−−−+member:Ref8612−−−−−−−−−−−−−−−−−−−−−−−>|   (object)   |                  +−−−−−−−−−−−−+      +−−−−−−−−−−−−−−+arr:Ref4875−−−−−−>|   (array)  |      | id: 1        |                  +−−−−−−−−−−−−+      | name: "John" |                  | 0: Ref4631 |−−+   +−−−−−−−−−−−−−−+                  +−−−−−−−−−−−−+  |                                   |                                   |                                   |                                   |                                  |   +−−−−−−−−−−−−−−+                                  +−−>|   (object)   |                                      +−−−−−−−−−−−−−−+                                      | id: 1        |                                      | name: "John" |                                      +−−−−−−−−−−−−−−+

关于javascript - 当该成员从数组中删除时,我们如何访问该成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54971533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com