custom-function
Version:
Literally the only sane way, if not the fastest one, to extend the Function class without evaluation
24 lines (22 loc) • 728 B
JavaScript
;
const {setPrototypeOf} = Object;
/**
* @param {Function} Class any base class to extend without passing through it via super() call.
* @returns {Function} an extensible class for the passed one.
* @example
* // creating this very same module utility
* import custom from 'custom-function/factory';
* const CustomFunction = custom(Function);
* class MyFunction extends CustomFunction {}
* const mf = new MyFunction(() => {});
*/
module.exports = Class => {
class Custom {
constructor(fn) {
return setPrototypeOf(fn, this.constructor.prototype);
}
}
// I am really sorry you are still using closure compiler
setPrototypeOf(Custom.prototype, Class.prototype);
return Custom;
};