Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active December 4, 2016 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burdiuz/36db19d655d94567ac278534fcacd239 to your computer and use it in GitHub Desktop.
Save burdiuz/36db19d655d94567ac278534fcacd239 to your computer and use it in GitHub Desktop.
Callable object
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
});
};
})();
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