Пример реализации
function sayHello(name) { console.log("Hello, " + name + "!"); } function staticHook(ptr) { console.log("statically hooked"); return ptr; } function runtimeHook(ptr) { return function(...args) { console.log("runtime hooked"); ptr(...args); } } const hello1 = staticHook(sayHello); const hello2 = runtimeHook(sayHello); hello1("World"); hello1("World"); hello2("Universe"); hello2("Universe"); console.log("-----------"); //----------- class Testus { constructor(name) { this.name = name; } sayHello() { console.log("Hello, " + this.name + "!"); } } function configHook(config, callable) { return function(...args) { let inst = new callable(...args); for (let i in config) { if (inst.hasOwnProperty(i)) { inst[i] = config[i]; } } return inst; } } //------------ const config = {name: "Guest"}; const TestusHooked = configHook(config, Testus); let inst = new TestusHooked("Admin"); inst.sayHello();
Вывод в консоль
statically hooked Hello, World! Hello, World! runtime hooked Hello, Universe! runtime hooked Hello, Universe! ----------- Hello, Guest!
Как можно использовать
import classHook from "./classHooks" class HookedClass {} export default classHook(HookedClass);