PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to send a callback from a javascript class js

 October 25, 2022     class, javascript, oop     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing