Extending a base class


#1

When calling an API we pass in multiple parameters to a function. e.g: the add function has 3 parameters

CommandRegistry ::add(target, commandName, callback) 

I’m creating an electron app like Atom. I just thought that what if I make use of a class as a parameter. e.g;

CommandRegistry ::add(CommandHandler);

The individual parameters would become properties of my base class and I can provide base implementation for callbacks. Sample below (sorry if I didn’t put in CoffeeScript)

class CommandHandler{
  constructor(args){
    this.commandName = args.CommandName //eg: "KeyboardHandler";
    this.target = args.target //"editor"
    this.callback = args != null ? args : run;
  }
 
  run(){
  }
}

I wanted developers to also create packages in my elecron app. The codes in the packages can inherit this base class and provide their own behavior.

//Some other package's class. e.g: TouchCommandHandler.js
class TouchCommandHandler extends CommandHandler {
  constructor(){
  super();
  this.commandName = "TouchHandler";
 }
}

Question is, if CommandHandler is defined in my electron app how can other packages access and extend this class? How would they do a: require(‘CommandHandler’) on it? Does Atom have a similar behavior?


#2

I had a similar question some time ago, see here: