Invoking a method from an Object key

I have this class

class Dark {
    constructor(name) {
        this.name = name;
    }
    destroy() {
       console.log("method called")
        console.log(this);
    }
}

const DarkObject = new Dark('DarkObject');

const copyDarkObject = {destroy: DarkObject.destroy}

console.log(copyDarkObject.destroy())

//> method called
// > undefined

I stored the reference of the class method to the key of a other Object

const copyDarkObject = { destroy: DarkObject.destroy };

the created Object is not the owner of the method so the return is undefined thats clear to me but I can still invoke the method from the key with console.log(copyDarkObject.destroy())

how is that possible ?

105 thoughts on “Invoking a method from an Object key”

  1. Pingback: generic priligy
  2. Pingback: ivermectin order
  3. Pingback: ic stromectol 6

Leave a Comment