Is it possible to delete the object itself, not the reference

别等时光非礼了梦想. 提交于 2020-01-01 10:09:26

问题


var a = {
    "example" : true
};

var x = [a], y = [a];

delete x[0];

console.log(y);

In the above code, would it be possible to have a deleted, not just the reference in x ?


回答1:


That's up to the garbage collector. As long as there's some reference to the object, it will not be garbage collected.

If you want it to be cleaned up, make sure there are no more references.

So to answer your question, no, there's no way to explicitly destroy an object. If a and y[0] are still referencing it, you can't do it from your x variable.

To be clear, x[0] is not referencing a. It is pointing to the same object in memory that a is referencing.




回答2:


i think you should refer this question.

Deleting Objects in JavaScript



来源:https://stackoverflow.com/questions/5097450/is-it-possible-to-delete-the-object-itself-not-the-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!