fahim545
fahim545

Reputation: 382

Is there a way to proxy (intercept) all methods of a class in javascript?

I want to be able to proxy all methods of a class inside the constructor of a class itself.

class Boy {
    constructor() {
        // proxy logic, do something before each call of all methods inside class
        // like if arg passed is 3, print something additionally
    }

    run(meters) {
        console.log(meters)
    }

    walk(meters) {
        // walk
    }
}

const myBoy = new Boy();
console.log(myBoy.run(3)) // should print 3 and something else

I think a for loop for each method would be an interesting approach, but at that point I could just implement the logic in the first lines of each function.

Upvotes: 9

Views: 7120

Answers (2)

tech-e
tech-e

Reputation: 462

They have added the apply method on proxies that will catch method calls. enter link description here

function sum(a, b) {
    return a + b;
}

const handler = {
    apply: function (target, thisArg, argumentsList) 
    {
        console.log(`Calculate sum: ${argumentsList}`);
        // Expected output: "Calculate sum: 1,2"

        return target(argumentsList[0], argumentsList[1]) * 10;
    },
};

const proxy1 = new Proxy(sum, handler);

console.log(sum(1, 2));
// Expected output: 3
console.log(proxy1(1, 2));
// Expected output: 30

Upvotes: -1

fahim545
fahim545

Reputation: 382

I realized I could just create a proxy with the target as the class object itself and then index the method.

class Boy {
    constructor() {
        // proxy logic, do something before each call of all methods inside class
        // like if arg passed is 3, print something additionally
        return new Proxy(this, {
            get(target, prop) {
                const origMethod = target[prop];
                if (typeof origMethod == 'function') {
                    return function (...args) {
                        if (args[0] == 3) {
                            return "3 is unlucky, you didn't go anywhere."
                        }
                        let result = origMethod.apply(target, args)
                        return result
                    }
                }
            }
        })
    }

    run(meters) {
        return `you ran ${meters}!`
    }

    walk(meters) {
        return `you walked ${meters}!`
        // walk
    }
}

const myBoy = new Boy();
console.log(myBoy.run(2)) // prints "you ran 2!"
console.log(myBoy.walk(3)) // prints "3 is unlucky, you didn't run."
console.log(myBoy.run(3)) // prints "3 is unlucky, you didn't run."

Upvotes: 16

Related Questions