1

Given the following code:

let obj = { data: 1 };
let ref1 = { data: obj };
let ref 2 = [ obj ];

How can I delete obj is such a way that it's references are also removed? (I mean that ref1.data === null && ref2.length === 0)

Is it possible?

4
  • 2
    No, it is not possible. Commented Feb 26, 2019 at 17:11
  • You can remove all the properties of it, but you can not just wipe it clean. Commented Feb 26, 2019 at 17:13
  • 1
    Put everything into a function, all the refrences will be wiped out after the function has been executed. Commented Feb 26, 2019 at 17:57
  • I can't put all the references into a function because they are in different parts of the application. Commented Feb 28, 2019 at 2:37

3 Answers 3

1

Somewhere inside the javascript execution environment there is some piece of code that is keeping track of references to objects and running memory garbage collection when all the references go out of scope.

You are looking for something similar, except you want to somehow find all active references to a particular object at a particular point in time, and change that reference to null.

I don't think that's possible, unless you want to write your own javascript interpreter.

But perhaps if you back up a level and explain why you want to do this, there is a way to achieve your goal.

Sign up to request clarification or add additional context in comments.

2 Comments

I have thought about this for hours and reached the same conclussion... The issue is that I have different collections of resources inside an application (they are not all equal, because they have different combiantions of this resources). I want to delete one of this resources in such a way that it is removed from all these collections, but I think it's not possible... I will have to update them one by one to remove the reference.
Not sure I fully understand your application, but how about this: a) Have a single "master store" of your "resource" objects. b) In all your different "collections", don't store the "resource" objects themselves, but rather just store a reference (e.g. an index or hash or key) that allows the object to be located in the "master store". c) When you want to globally delete a resource, delete it from the master store, which will cause any attempt to look it up from any of the "collections" to return a null or throw an error.
0

You can remove the property from the object:

let obj = {
  data: 1
};
let ref1 = {
  data: obj
};
let ref2 = [obj];

delete obj.data;

console.log(obj);
console.log(ref1.data);

Comments

0

You can not delete all references of object. Setting obj=null will not have impact on its references. You can use delete keyword to delete properties of object.

let obj = { data: 1 };
let ref1 = { data: obj };
let ref2 = [ obj ];
delete obj; // no effect
console.log(obj.data); // still have 1
console.log(ref1.data); // still have obj value
obj=null;
console.log(obj)  //null
console.log(ref1.data)  //still have obj value

1 Comment

This is what I am actually doing, but I was wondering if it was possible to completely remove the object (actually, I still have the empty object inside the array and ref1.data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.