Last active
December 13, 2021 10:10
-
-
Save XoseLluis/94e071c659cbbb3728db to your computer and use it in GitHub Desktop.
ES6 proxies and method interception, 2 different ways of setting your get trap
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
// http://deploytonenyures.blogspot.fr/2015/11/es6-proxies-part-ii.html | |
//to run it in node.js 4 it should be just this flag: --harmony_proxies | |
//but does not seem to work, so run it in Firefox | |
var cat = { | |
name: "Kitty", | |
method1: function(msg){ | |
console.log("cat: " + this.name + ", method1 invoked with msg: " + msg); | |
this.method2(msg); | |
}, | |
method2: function(msg){ | |
console.log("cat: " + this.name + ", method2 invoked with msg: " + msg); | |
} | |
}; | |
console.log("------------------------------"); | |
console.log("- No Proxy:"); | |
cat.method1("Francois"); | |
console.log("------------------------------"); | |
//other method calls done from the first intercepted method are ALSO intercepted | |
var allCallsProxied = new Proxy(cat, { | |
get: function(target, propKey, receiver){ | |
//I only want to intercept method calls, not property access | |
var propValue = target[propKey]; | |
if (typeof propValue != "function"){ | |
return propValue; | |
} | |
else{ | |
return function(){ | |
console.log("intercepting call to " + propKey + " in cat " + target.name); | |
//"this" points to the proxy, is like using the "receiver" that the proxy has captured | |
return propValue.apply(this, arguments); | |
} | |
} | |
} | |
}); | |
console.log("------------------------------"); | |
console.log("- Using Proxy 1:"); | |
allCallsProxied.method1("Francois"); | |
console.log("------------------------------"); | |
//other method calls done from the first intercepted method are NOT intercepted | |
var entryCallProxied = new Proxy(cat, { | |
//target is the object being proxied, receiver is the proxy | |
get: function(target, propKey, receiver){ | |
//I only want to intercept method calls, not property access | |
var propValue = target[propKey]; | |
if (typeof propValue != "function"){ | |
return propValue; | |
} | |
else{ | |
return function(){ | |
console.log("intercepting call to " + propKey + " in cat " + target.name); | |
//target is the object being proxied | |
return propValue.apply(target, arguments); | |
} | |
} | |
} | |
}); | |
console.log("------------------------------"); | |
console.log("- Using Proxy 2:"); | |
entryCallProxied.method1("Francois"); | |
console.log("------------------------------"); | |
// ------------------------------ | |
// - Using allCallsProxied: | |
// intercepting call to method1 in cat Kitty | |
// cat: Kitty, method1 invoked with msg: Francois | |
// intercepting call to method2 in cat Kitty | |
// cat: Kitty, method2 invoked with msg: Francois | |
// 2 | |
// ------------------------------ | |
// - Using entryCallProxied: | |
// intercepting call to method1 in cat Kitty | |
// cat: Kitty, method1 invoked with msg: Francois | |
// cat: Kitty, method2 invoked with msg: Francois | |
// ------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment