Created
September 12, 2013 12:18
-
-
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.
This file contains 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
// 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