Last active
December 4, 2016 12:42
-
-
Save burdiuz/36db19d655d94567ac278534fcacd239 to your computer and use it in GitHub Desktop.
Callable object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wrapCallableObject = (() => { | |
const wrapper = () => { | |
throw new Error('Wrapper cannot be called'); | |
}; | |
return function wrapCallableObject(object) { | |
if (object) { | |
return object instanceof Function ? object : new Proxy( | |
wrapper, { | |
apply: (wrapper, thisObj, args) => { | |
object.apply(thisObj, args); | |
} | |
}); | |
} | |
}; | |
})(); | |
const wrapCallableObjectClass = (() => { | |
const construct = (target, args) => { | |
return wrapCallableObject(new target(...args)); | |
}; | |
return function wrapCallableObject(definition) { | |
return new Proxy( | |
definition, { | |
construct | |
}); | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AbstractCallableObject { | |
constructor() { | |
return (...args) => this.apply(this, args); | |
} | |
apply(target, args) { | |
this.call(target, ...args); | |
} | |
/*abstract*/ call(target, ...args) { | |
throw new Error('Abstract .call() method cannot be called.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment