Skip to content

Instantly share code, notes, and snippets.

@tvcutsem
Created September 12, 2013 12:18
Show Gist options
  • Save tvcutsem/6536442 to your computer and use it in GitHub Desktop.
Save tvcutsem/6536442 to your computer and use it in GitHub Desktop.
An ES6 proxy making use of "double-lifting" (i.e. a proxy whose handler is a proxy). Allows you to write generic handlers that only have to implement a single trap, yet can intercept all possible operations.
// a proxy making use of "double-lifting" (i.e. a proxy whose handler is a proxy)
var metaHandler = {
get: function(dummyTarget, trapName, dummyReceiver) {
return function(...trapArgs) { // function acting as a generic trap, its this-binding is irrelevant
console.log("intercepting "+trapName);
return Reflect[trapName](...trapArgs); // forward manually
}
},
};
var target = {x:1};
var dummy = {};
var doubleLiftedProxy = new Proxy(target, new Proxy(dummy, metaHandler));
doubleLiftedProxy.x // intercepting get, evals to 1
'x' in doubleLiftedProxy // interecepting has, evals to true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment