Created
April 19, 2011 00:28
-
-
Save DavidBruant/926581 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
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