Can an object automatically delete itself in javascript once it has achieved its purpose?

后端 未结 3 2014
太阳男子
太阳男子 2020-12-08 04:03

I am wondering if it is possible for an object in javascript to delete itself once it has finished its task.

For example, I have the following object...



        
3条回答
  •  失恋的感觉
    2020-12-08 05:03

    That wouldn't work, first because the this value associated with an execution context is immutable.

    You might now think that deleting myObject (by delete myObject;) might work, but that wouldn't do it either.

    Variables are really properties of the Variable Object, this object is not accessible by code, it is just in front of in the scope chain, where you do the variable declarations.

    The Variable statement, creates those properties with the { DontDelete } attribute, and that causes the delete operator to fail.

    An option if you want to achieve this is to nullify your myObject instance, but that doesn't guarantees that another reference is still pointing to that object.

    Recommended lectures:

    • Understanding delete
    • Variable instantiation

提交回复
热议问题