How to attach toString to proxy handler

51 Views Asked by At

Consider this code:

handler.toString = function() {
    return Reflect.get(original,'toString');
}
handler.apply = scope.exportFunctionWithName(function(target, thisArgs, args){
    try {
        return args.length?
            replacement.call(thisArgs, ...args):
            replacement.call(thisArgs);
    }
    catch (error){
        try {
            return original.apply(thisArgs, args);
        }
        catch (error){
            return target.apply(thisArgs, args);
        }
    }
}, window, "");
const proxy = new window.Proxy(original, handler);

if I invoke ApiObj.toString(), it works as expected.

Now if I want to invoke ApiObj.toString.caller, it errors out, with

get caller method called on incompatible proxy

How can I fix this ?

0

There are 0 best solutions below