Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Created February 19, 2017 23:08
Show Gist options
  • Save melbourne2991/7e58618694e68fdef8e107f0f1d59622 to your computer and use it in GitHub Desktop.
Save melbourne2991/7e58618694e68fdef8e107f0f1d59622 to your computer and use it in GitHub Desktop.
Fun with proxies :)
const HeaderAPI = {};
HeaderAPI.updateAuthenticated = function() {
console.log('UPDATING!');
}
class Header {
constructor() {
}
}
Header.prototype = new Proxy(Header.prototype, {
get(target, name) {
const proxiedMethods = ['updateAuthenticated'];
if(proxiedMethods.some((method) => method === name)) {
if(!window.HeaderAPI) {
return () => { console.log('This method is not defined!') }
}
return window.HeaderAPI[name];
}
return target[name];
}
})
const proxyHeader = new Header();
proxyHeader.updateAuthenticated();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment