简体   繁体   English

如何删除一个javascript对象的每个引用?

[英]How delete every reference of a javascript object?

In nodes terminal: 在节点终端中:

> o={a:1}                                                                                                                                                                        
{ a: 1 }                                                                                                                                                                         
> p=o        //set p=o(if I change anything in p or o, it reflects to the references                                                                                                                                                                    
{ a: 1 }                                                                                                                                                                         
> p=null     //but  setting p=null                                                                                                                                                                         
null                                                                                                                                                                             
> o                                                                                                                                                                              
{ a: 1 }     //don't set o to null

So I ask, why this happen, and more important, through one reference(or the original) can I delete every reference to that memory location? 所以我问,为什么会发生这种情况,更重要的是,通过一个引用(或原始引用)可以删除对该内存位置的每个引用吗?

So this way, I can delete 'o' when doing something to p? 这样,我可以对p做某事时删除'o'吗?

Thanks 谢谢

why this happens 为什么会这样

References and objects are different things. 引用和对象是不同的东西。 You reassigned p to point to null (conceptually). 您已重新分配p使其指向null (概念上)。 That did not affect other references. 那没有影响其他参考。

through one reference(or the original) can I delete every reference to that memory location? 通过一个引用(或原始引用)可以删除对该存储位置的每个引用吗?

Nope, that is not possible. 不,那是不可能的。

Why this happens: 为什么会这样:

p and o are technically not objects, they are references to an object. po从技术上讲不是对象,它们是对对象的引用。 The object exists as long as at least one reference to it exists.* 只要存在至少一个对该对象的引用,该对象就存在。*

When you set p to null, you're simply changing p so that it no longer references the same object as o - you're changing the value of the reference, not the value of the object. 将p设置为null时,您只是在更改p,以使其不再引用与o相同的对象-您在更改引用的值,而不是对象的值。 There is no mechanism by which you can get rid of all references to the object except to set each reference individually to null. 除了将每个引用分别设置为null之外,没有任何机制可以摆脱对对象的所有引用。

You could, however, use any reference to delete all the properties on the object, eg you could do: 但是,您可以使用任何引用来删除对象上的所有属性,例如,您可以这样做:

delete p.a;

which would make o reference a now-empty object. 这将使o引用现在为空的对象。

*It's a little more complex than that, as modern JavaScript engines can and do garbage-collect circular references correctly. *这要复杂得多,因为现代JavaScript引擎可以并且可以正确进行垃圾回收循环引用。

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

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