1

As many of you are aware, the javascript delete keyword is a little tricky to use (see here). Is it possible to re-implement, or modify it? I have an object referenced multiple times and I want delete to remove all the references first. Like so:

var oj = new OrangeJuice();
var juice = oj;
var beverage = oj;
var allRightSunnyD = oj;
delete oj; //I want this to delete the actual object

I do not expect the garbage collector to find all of the references, lets say I know where the references are, i just want to re-implement delete to also get rid of juice, beverage and allRightSunnyD. I realize I could just implement a OrangeJuice.delete() function, but I wanted to know if there was a way to do it right. Like if javascript would call an onDelete() callback function prior to deleting objects.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
puk
  • 16,318
  • 29
  • 119
  • 199

1 Answers1

7
  1. delete is a keyword, so it should not be altered even if you can
  2. delete is for deleting properties of objects, so you can not delete objects contained in single variables

look here how garbage collection in Javascript is explained: How does garbage collection work in JavaScript?

Community
  • 1
  • 1
Alex
  • 5,240
  • 1
  • 31
  • 38
  • I knew it was a long shot to begin with. quick question, if we have an object `oj` and set a property of it like so `oj.squeeze = true` then reference it like so `var squeeze = oj.squeeze` then delete it like so `delete oj.squeeze`, what will the variable `squeeze` now point to? What if instead of setting it to true we had set it to an immutable object, such as `[true]`? – puk Jul 07 '11 at 17:32
  • 1
    well this is the key of the problem: squeeze will still point to "true" since the memory can not be freed unless there is nothing pointing to it anymore. This is the reason because declaring variables with var is important. Otherwise variables will be declared in the global namespace and stay there for your whole runtime. – Alex Jul 07 '11 at 20:03
  • Is there a reason they don't also give you the option to actually delete the object? Some function like `DELETE` or `_delete`. – puk Jul 07 '11 at 22:28
  • Since you can iterate over Javascript properties it may be useful to remove them, however I did never actually use it. If you freed memory, other objects or variables would point to somewhere they should not or are assigned null - which causes errors either ways, handling them is more difficult than it seems, especially in large programs where you might not see who actually deleted the object you are pointing to. If a program is prohibited to access certain objects it has to check this by other means than to free memory. – Alex Jul 08 '11 at 06:33
  • 1
    And above all: you are able to delete an object if all references to it are deleted as well. Only then it is actually possible to remove it from memory. But actually I am happy that I am not forced to take care of this. I either try to avoid unnecessary holding objects in global namespace and good Javascript implementations should take care of the rest all by themselves. – Alex Jul 08 '11 at 06:39