相关疑难解决方法(0)

为什么ES2015中的Map对象的Proxy无法正常工作

我通过Google Chrome版本57.0.2987.133运行以下脚本:

var loggingProxyHandler = {
    "get" : function(targetObj, propName, receiverProxy) {
        let ret = Reflect.get(targetObj, propName, receiverProxy);
        console.log("get("+propName.toString()+"="+ret+")");
        return ret;
     },

     "set" : function(targetObj, propName, propValue, receiverProxy) {
         console.log("set("+propName.toString()+"="+propValue+")");
         return Reflect.set(targetObj, propName, propValue, receiverProxy);
     }
};

function onRunTest()
{
    let m1 = new Map();
    let p1 = new Proxy(m1, loggingProxyHandler);
    p1.set("a", "aval");   // Exception thrown from here
}

onRunTest();
Run Code Online (Sandbox Code Playgroud)
NOTE: Requires a browser supporting ES2015's Proxy
Run Code Online (Sandbox Code Playgroud)

运行时,我看到调用处理程序的get陷阱来返回Map的set函数,然后我收到以下错误:

"Uncaught TypeError: Method Map.prototype.set called on incompatible receiver [object Object]"
at Proxy.set …
Run Code Online (Sandbox Code Playgroud)

javascript proxy dictionary google-chrome

3
推荐指数
2
解决办法
1793
查看次数

标签 统计

dictionary ×1

google-chrome ×1

javascript ×1

proxy ×1