How do I trap arguments to a target method when using a Proxy object?

后端 未结 2 1374
-上瘾入骨i
-上瘾入骨i 2021-02-05 12:56

I\'m trying to use Javascript Proxy objects to trap the arguments that are passed to a \'method\' of the target that I\'m proxying.

Please consider this example:

2条回答
  •  生来不讨喜
    2021-02-05 13:22

    another snippet : )

    const obj_hidden = {};
    
    const obj = new Proxy(obj_hidden, {
        get(target, prop) {
            if (typeof target[prop] == 'function') {
              return function (...args) {
                console.dir({ call: [prop, ...args] });
                return target[prop].apply(target, args);
              }
            }
            console.dir({ get: prop });
            return target[prop];
        },
        set(target, prop, value) {
            console.dir({ set: [prop, value] });
            target[prop] = value;
            return true;
        }
    });
    

提交回复
热议问题