Skip to content

Instantly share code, notes, and snippets.

@DrewML
Created September 4, 2016 20:23
Show Gist options
  • Save DrewML/c195daba750e50566f1d2fd61d3515c8 to your computer and use it in GitHub Desktop.
Save DrewML/c195daba750e50566f1d2fd61d3515c8 to your computer and use it in GitHub Desktop.
const window = {
a: 1
};
global = new Proxy(global, {
get(target, prop, receiver) {
if (target[prop] !== undefined) {
return target[prop];
}
return window[prop];
}
});
// Will be caught by proxy
console.log(global.a);
// Will not be caught by proxy. I'm _guessing_ the
// [[Get]] slot isn't being used when resolving against the
// global environment record?
console.log(a);
// This works as intended. Referencing `a` in the top-level
// calls the getter on the `global` object
Object.defineProperty(global, 'a', {
get() {
return 'this works';
}
});
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment