Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created April 19, 2011 00:28
Show Gist options
  • Save DavidBruant/926581 to your computer and use it in GitHub Desktop.
Save DavidBruant/926581 to your computer and use it in GitHub Desktop.
Proxy.ForwardingHandler.prototype = {
/***
** all traps use this.state.get[proxy] to retrieve the target
*/
state: new WeakMap();
};
function ForwardingPair(target){// Could be Proxy.ForwardingPair too...
var h = new Proxy.ForwardingHandler(); // Just a new object identity inheriting from all methods
var p = Proxy.create(h);
h.state.set(p) = target;
return {handler: h, proxy: p};
}
// ... //
var target = {};
var {handler: h, proxy: p} = ForwardingPair(target); // destructuring is convenient here
// changing one trap
h.set = function(rec, name, value, proxy){
var target = this.state.get(proxy); // reaching internal state from the new trap
console.log("New set trap in forwarding proxy. Property name: "+name);
target[name] = value;
};
// ... //
p.ProxiesAreAwesome = 42;
// logs: "New set trap in forwarding proxy. Property name: ProxiesAreAwesome"
target.ProxiesAreAwesome; // 42. Forwarding went well too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment