Skip to content

Instantly share code, notes, and snippets.

@claudioc
Last active April 19, 2018 19:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claudioc/644ee66d0a61bc54a671a8206f08cee6 to your computer and use it in GitHub Desktop.
Save claudioc/644ee66d0a61bc54a671a8206f08cee6 to your computer and use it in GitHub Desktop.
/* This can be the start of a module */
const makeHandler = cb => {
return {
get (object, prop, receiver) {
if (Reflect.has(object, prop)) {
return Reflect.get(...arguments)
}
return new Proxy(() => {}, {
apply (method, thisArg, args) {
cb.apply(object, [object, prop, ...args])
}
})
}
}
}
const withMethodMissing = (obj, cb) => {
return new Proxy(obj, makeHandler(cb))
}
/* End of the 'withMethodMissing' module */
class FooBar {
}
const myFooBar = withMethodMissing(new FooBar(), (obj, method, ...args) => {
console.log(`You have tried calling the missing method "${method}" with`, args)
})
myFooBar.iDoNotExist('bazinga', 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment