相关疑难解决方法(0)

如何可靠地检查对象是EcmaScript 6 Map/Set?

我只是想检查的对象是MapSet,而不是一个Array.

检查我正在使用lodash的数组_.isArray.

function myFunc(arg) {
  if (_.isArray(arg)) {
    // doSomethingWithArray(arg)
  }

  if (isMap(arg)) {
    // doSomethingWithMap(arg)
  }

  if (isSet(arg)) {
    // doSomethingWithSet(arg)
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我要实现isMap/ isSet,它需要看起来像什么?我希望它能够捕获Map/的子类,Set如果可能的话.

javascript collections dictionary set ecmascript-6

8
推荐指数
2
解决办法
1259
查看次数

如何在 Javascript 中的“window.location”上应用“get”代理?

我正在使用Chrome 60。我刚刚尝试在 上应用get 代理window.location

\n

它适用于前两个参考,但是,随后失败并出现Illegal invocation错误:

\n
location = new Proxy(location, {\n    get: (target, name) => {\n        console.log(name, target, "PROX");\n        return target[name];\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n

错误消息是:

\n
\n

VM3495:3 符号(Symbol.toPrimitive) 位置 {\xe2\x80\xa6} "PROX"

\n

VM3495:3 toString 位置 {\xe2\x80\xa6} PROX

\n

未捕获的类型错误:非法调用:1:10

\n
\n
    \n
  1. 为什么它会抛出错误?
  2. \n
  3. 如何在 Javascript 中应用get代理window.location
  4. \n
\n

javascript

4
推荐指数
1
解决办法
4213
查看次数

为什么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
查看次数