How to test if an object is a Proxy?

后端 未结 13 2199
予麋鹿
予麋鹿 2020-11-30 07:23

I would like to test if a JavaScript object is a Proxy. The trivial approach

if (obj instanceof Proxy) ...

doesn\'t work here, nor does tra

13条回答
  •  天命终不由人
    2020-11-30 07:45

    Use window.postMessage() with try-catch

    postMessage cannot serialize objects which incompatible with structured clone algorithm, like Proxy.

    function isProxy(obj) {
        try {
            postMessage(obj, "*");
        } catch (error) {
            return error && error.code === 25; // DATA_CLONE_ERR
        }
    
        return false;
    }
    

提交回复
热议问题