Child of Function

node v6.17.1
version: 1.1.0
endpointsharetweet
http://stackoverflow.com/a/36871623/5244995
Symbol.call = Symbol.call || Symbol('call'); Symbol.construct = Symbol.construct || Symbol('construct'); class Callable extends Function { constructor(name) { super(); // source: http://stackoverflow.com/a/36871623/5244995 // Generalized by J F, https://runkit.com/j-f • http://stackoverflow.com/u/5244995 // Support: http://caniuse.com/#feat=proxy (Edge 12+, Firefox 18+, Chrome 36+, Safari 10+) // There does not appear to be a polyfill. const pxy = new Proxy(this, { apply(target, thisArg, args) { return (target[Symbol.call] || (() => {})).apply(target, [thisArg, ...args]); }, construct(target, args, newTarget) { return (target[Symbol.construct] || (() => {})).apply(target, [newTarget, ...args]) || newTarget; } }); Object.defineProperty(pxy, 'name', {value: name}) return pxy } }
class Smth extends Callable { constructor(x) { super('hello'); this._x = x; } [Symbol.call]() { return this._x } [Symbol.construct](newTarget) { newTarget._x = this._x; } } const smth = new Smth(256); // 256
smth()
typeof smth
smth instanceof Smth
smth instanceof Function
Object.getPrototypeOf(smth)
smth.name = 'test' smth.name
const newsmth = new smth()
newsmth()
Loading…

no comments

    sign in to comment