JSFiddle - React, Tailwind, and code Playground
JavaScript
var PluginBase = function(name, things){
this.name = name;
this.things = things;
}
/// First variant
var Plugin = function() {
this.constructor.apply(this, arguments);
}
Plugin.prototype.constructor = PluginBase;
Plugin.prototype.has = function(str,member){
if(this.things.indexOf(str) > -1) {
return this;
} else {
var tmp = {}
tmp[member] = function(){}
return tmp;
}
}
var plugin_with_afk = new Plugin("with afk", ["afk"]);
plugin_with_afk.check = function(val){
console.log("hi", val, this.name);
};
var plugin_without_afk = new Plugin("w/o afk", ["nope"]);
plugin_without_afk.check = function(val){
console.log("nope", val, this.name);
}
/// First variant demo
plugin_with_afk.has("afk","check").check(1)
plugin_without_afk.has("afk","check").check(2)
plugin_without_afk.has("nope","check").check(3)
/// Alternative
var PluginWithFuncRet = function(){
this.constructor.apply(this, arguments);
}
PluginWithFuncRet.prototype.constructor = PluginBase;
PluginWithFuncRet.prototype.has = function(str,member){
var self = this;
if(this.things.indexOf(str) > -1) {
return function(){
return self[member].apply(self,arguments);
}
} else {
return function(){}
}
}
plugin_with_afk = new PluginWithFuncRet("with afk",["afk"]);
plugin_with_afk.check = function(val){
console.log("Hi",val,this.name);
}
plugin_without_afk = new PluginWithFuncRet("w/o afk",["nope"]);
plugin_without_afk.check = function(val){
console.log("Nope",val,this.name);
}
/// Alternative demo
plugin_with_afk.has("afk","check")(4);
plugin_without_afk.has("afk","check")(5);
plugin_without_afk.has("nope","check")(6);