Home > front end >  How to send a callback from a javascript class js
How to send a callback from a javascript class js

Time:10-19

I'm trying to send a callback from a javascript class to its object. I haven't gotten anywhere with it.

class MyClass {
    constructor(param1, param2) {
      // trigger the callback
      this.callback();
   }
}

obj = MyClass({
    parameter1: 'test',
    parameter2: 'test',
    callback() {
        alert('callback received');
    }
});

CodePudding user response:

You can just pass a callback function as an argument to the constructor:

class MyClass {
  constructor(callback) {
    callback();
  }
}

new MyClass(() => console.log('Called back'));

  • Related