misterbobot
misterbobot

Reputation: 92

How to delete object and all references?

We use the delete when we need to delete the object reference. But is there any way in JS to delete the entire object and all references?

Example:

let obj = {
    a: 3
}
let arr = [obj];
let brr = [obj];

If I'd change the obj items in the arrays would be changed too. Also, I can use delete arr[0] to delete the reference. But I want to delete the obj and I want the items in the arrays to be deleted too.

Something like:

// Before: 
let obj = {
   a: 3
}
let arr = [obj];
let brr = [obj];
arr.length==1 // true
brr.length==1 // true
// Deleting:
delete(obj)
// After:
arr.length==0 // true
brr.length==0 // true

Upvotes: 0

Views: 733

Answers (1)

Quentin
Quentin

Reputation: 943995

No.

The only way to delete an object in JavaScript is to locate and delete each reference to it.

There's no generic mechanism to start with an object and automatically delete all references to it.

Upvotes: 2

Related Questions