Last active
April 19, 2018 19:27
-
-
Save claudioc/644ee66d0a61bc54a671a8206f08cee6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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