UNPKG

custom-function

Version:

Literally the only sane way, if not the fastest one, to extend the Function class without evaluation

23 lines (21 loc) 712 B
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(() => {}); */ export default 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; };