Applying method interception with JavaScript Proxy

In JavaScript, the Proxy object provides a way to intercept and customize the behavior of operations performed on target objects. This powerful feature allows developers to modify object behavior, including method calls, by wrapping the target object with a Proxy.

To demonstrate method interception with a Proxy, let’s consider a simple example where we want to log method calls of an object. We can achieve this by creating a Proxy that intercepts method invocations and logs the method name and arguments.

// Create an object to be proxied
const targetObject = {
    greeting: 'Hello',
    sayHello(name) {
        console.log(this.greeting + ' ' + name + '!');
    }
};

// Create a Proxy to intercept method calls
const proxy = new Proxy(targetObject, {
    get(target, property) {
        const value = target[property];
        if (typeof value === 'function') {
            return function (...args) {
                console.log('Method call:', property);
                console.log('Arguments:', args);
                return value.apply(target, args);
            };
        }
        return value;
    }
});

// Make method calls on the proxied object
proxy.sayHello('John');

In this example, we create a targetObject with a sayHello method which logs a greeting message. We then create a Proxy object proxy which intercepts method calls using the get trap. Inside the trap, we check if the property being accessed is a function. If it is, we log the method name and arguments before invoking the original method using .apply() to maintain the correct context.

When we invoke the sayHello method on the proxy object, it logs the method call and arguments before executing the original method. This allows us to modify the behavior of the object without modifying the original object itself.

Using the Proxy object, method interception becomes a powerful tool for implementing various advanced patterns such as logging, validation, caching, and access control. It provides a flexible way to customize and modify the behavior of objects, making JavaScript code more maintainable and extensible.

#JavaScript #Proxy