0

I have an external javascript class that accepts a callback method parameter. When the callback is run, it is like the API steals the method and tries to run within itself.

I have an example below. The idea is the API should be able to announce the manager to be "Manager" but instead it announces itself("API").

class Manager{
  constructor(){
    this.name = "Manager";
  }
  
  manage(){
    var api = new API(this.announce);
  }
  
  announce(){
    alert(this.name);
  }
}

class API{
  constructor(announceCallback){
    this.name = "API";
    this.announceManager = announceCallback;
    this.announceManager();
  }
}

manager = new Manager();
manager.manage();

How do I access "this" from an external class in Javascript. I have read that JS can lose implicit binding. I've also been reading about call() and apply(), but these appear to only work with functions and not class methods. How can I modify my "Manager" class so when API calls the "announceCallback" it alerts "Manager" rather than "API". I'm specifically needing resolution for use in a class and not independent functions as described in another post.

bristweb
  • 948
  • 14
  • 14

0 Answers0