For a Proxy, function signature looks like:
const target = {} const handler = { get(target, propKey) { return Reflect.get(target, propKey) } } const proxy = new Proxy(target, handler)
We implement handler with 'get' function. But there is still lots of function which can be implemented. Docs
To make it easy, to can use another Proxy to impelemnt the handler:
const handler = new Proxy({}, { get(target, trapName. receiver) { // Return the handler method named trapName return (...args) => { return Reflect[trapName](...args); }; }, });
The reason that we can use 'Reflect' is because, Proxy's handler and Reflect API are the same.
Using:
const handler = new Proxy({}, { get(target, trapName, receiver) { // Return the handler method named trapName return (...args) => { console.log(trapName.toUpperCase() + ' ' + args[1]); // Forward the operation return Reflect[trapName](...args); }; }, }); const target = {}; const proxy = new Proxy(target, handler); proxy.distance = 450; // set assert.equal(proxy.distance, 450); // get