[removed] Create and destroy class instance through class method

后端 未结 3 1197
抹茶落季
抹茶落季 2021-02-03 18:16

I\'m trying to figure out how to delete an object through a class method. I would like to be able to create a class that has a destroy method that releases the object from memor

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-03 18:41

    1- There is no way to actually destroy an object in javascript, but using delete, we could remove a reference from an object:

    var obj = {};
    obj.mypointer = null;
    delete obj.mypointer;
    

    2- The important point about the delete keyword is that it does not actually destroy the object BUT if only after deleting that reference to the object, there is no other reference left in the memory pointed to the same object, that object would be marked as collectible. The delete keyword deletes the reference but doesn't GC the actual object. it means if you have several references of the same object, the object will be collected just after you delete all the pointed references.

    3- there are also some tricks and workarounds that could help us out, when we want to make sure we do not leave any memory leaks behind. for instance if you have an array consisting several objects, without any other pointed reference to those objects, if you recreate the array all those objects would be killed. For instance if you have var array = [{}, {}] overriding the value of the array like array = [] would remove the references to the two objects inside the array and those two objects would be marked as collectible.

    4- for your solution the easiest way is just this:

    var storage = {};
    storage.instance = new Class();
    //since 'storage.instance' is your only reference to the object, whenever you wanted to destroy do this:
    storage.instance = null;
    // OR
    delete storage.instance;
    

    As mentioned above, either setting storage.instance = null or delete storage.instance would suffice to remove the reference to the object and allow it to be cleaned up by the GC. The difference is that if you set it to null then the storage object still has a property called instance (with the value null). If you delete storage.instance then the storage object no longer has a property named instance.

    and WHAT ABOUT destroy method ??

    the paradoxical point here is if you use instance.destroy in the destroy function you have no access to the actual instance pointer, and it won't let you delete it.

    The only way is to pass the reference to the destroy function and then delete it:

    // Class constructor
    var Class = function () {
         this.destroy = function (baseObject, refName) {
             delete baseObject[refName];
         };
    };
    
    // instanciate
    var storage = {};
    storage.instance = new Class();
    storage.instance.destroy(object, "instance");
    console.log(storage.instance); // now it is undefined
    

    BUT if I were you I would simply stick to the first solution and delete the object like this:

    storage.instance = null;
    // OR
    delete storage.instance;
    

    WOW it was too much :)

提交回复
热议问题