JSFiddle - React, Tailwind, and code Playground
JavaScript
const myObject = {
a: 'Hello world!',
b: x => x * x
};
const myProxyObject = new Proxy(myObject, {
get(target, propKey, receiver) {
// Calling functions
if (typeof target[propKey] === 'function') {
return new Proxy(target[propKey], {
apply(applyTarget, thisArg, args) {
console.log(`> Calling ${thisArg.constructor.name}.${propKey}(${args})`);
return Reflect.apply(applyTarget, thisArg, args);
}
});
}
// Accessing properties
if (target.hasOwnProperty(propKey)) {
console.log(`> Get value of ${target.constructor.name}.${propKey}`);
console.log(`> Value: ${target[propKey]}`);
}
return target[propKey];
}
});
console.log('myProxyObject.a = ' + myProxyObject.a);
console.log('myProxyObject.b(4) = ' + myProxyObject.b(4));