Skip to content

Instantly share code, notes, and snippets.

@seaneagan
Created April 19, 2011 18:30
Show Gist options
  • Save seaneagan/929185 to your computer and use it in GitHub Desktop.
Save seaneagan/929185 to your computer and use it in GitHub Desktop.
Proxy internal state API
Proxy.ForwardingHandler.prototype = {
create: function(target) {return {target: target}},
getPrototypeOf: function (proxy, state) {
return Object.getPrototypeOf(state.target);
}
// all other traps take state argument
};
// ... //
var target = {}, h = new Proxy.ForwardingHandler, P = Proxy.Constructor(h), p = P(target);
// changing one trap
h.set = function(...args){
console.log("New set trap in forwarding proxy. Property name: " + args[1]);
Proxy.ForwardingHandler.set.apply(this, args);
};
// ... //
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