0

I would like provide to our future object a method which can selfdestroy/hide or something like that. How can i do It ? Thank

class BuildRobot {
    constructor(size, model , color){
        this.size = size;
        this.model = model
        this.color =color;
        this.selfdestroy = function ( ?? ) {
            delete/remover/hide ??;
        }
    } 
}

let robot1 = new BuildRobot("big", "mecha","s-1","blue")
robot1.selfdestroy 
console.log (robot1)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • JavaScript uses automatic memory management with garbage collection, there's no way to explicitly destroy objects. When there are no accessible references to the object, it's destroyed. – Barmar Aug 03 '22 at 01:00

1 Answers1

0

Maybe you can try this code:

class BuildRobot {
 constructor(size, model , color){
    this.size = size;
    this.model = model
    this.color =color;
    this.selfdestroy = function () {
        delete this.color
        delete this.model
        delete this.size
        delete this.selfdestroy
    }
  } 
}

let robot1 = new BuildRobot("big", "mecha","s-1","blue")
robot1.selfdestroy()
console.log (robot1)

Using the delete keyword will delete the reference to the property, but on the low level the JavaScript garbage collector (GC) will get more information about which objects to be reclaimed.

You could also use Chrome Developer Tools to get a memory profile of your app, and which objects in your app are needing to be scaled down.

reference: Link