Issue
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');
}
});
Solution
You can just pass a callback function as an argument to the constructor:
class MyClass {
constructor(callback) {
callback();
}
}
new MyClass(() => console.log('Called back'));
Answered By - Robby Cornelissen Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.