开发者

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

开发者 https://www.devze.com 2023-02-12 20:04 出处:网络
var a = { \"example\" : true }; var x = [a], y = [a]; delete x[0]; console.log(y); In the above code, would it be possible to 开发者_Go百科have a deleted, not just the reference in x ?That\'s up
var a = {
    "example" : true
};

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

delete x[0];

console.log(y);

In the above code, would it be possible to 开发者_Go百科have a deleted, not just the reference in x ?


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.


i think you should refer this question.

Deleting Objects in JavaScript

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号