Skip to content

Instantly share code, notes, and snippets.

@Nek
Last active August 6, 2023 13:02
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 Nek/5fea561b3c71da4f3a475aacfd5426f5 to your computer and use it in GitHub Desktop.
Save Nek/5fea561b3c71da4f3a475aacfd5426f5 to your computer and use it in GitHub Desktop.
class BackedProxy {
static DEBUG = true;
static WRAPPED = Symbol('Wrapped Objects accessor.');
static make = (obj, origObj) => new Proxy(obj, {
get(obj, prop, receiver) {
if (prop === BackedProxy.WRAPPED) return {obj, origObj};
if (BackedProxy.DEBUG) {
console.log(`get ${prop.toString()})`)
}
const value = prop in obj ? obj[prop] : origObj[prop]
if (value instanceof Function) {
return function (...args) {
return value.apply(this === receiver ? obj : this, args);
};
}
return value;
},
set(obj, prop, value, receiver) {
if (BackedProxy.DEBUG) {
console.log(`set ${prop.toString()})`)
}
if (prop in obj) {
obj[prop] = value;
return true;
} else if (prop in origObj) {
origObj[prop] = value;
return true;
}
return false;
}
})
}
window.BackedProxy = BackedProxy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment