Illegal invocation during non configurable method call with javascript proxies

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 19:16:54

问题


I am using javascript proxies to intercept method calls to an object, however if the method is a non configurable and non writable property, I am not able to correctly intercept it

var handler = {
  get(target, key, receiver) {
      if (target[key] && (typeof target[key] === 'object' || typeof target[key] === "function")) {
      var desc = Object.getOwnPropertyDescriptor(target, key);
      if (desc && ! desc.configurable && !desc.writable) return Reflect.get(target,key);
      var method = Reflect.get(target, key);
      if (typeof method == "function") {
        return function (...args) {
            return method.apply(target, args);
         }
        } else return new Proxy(method, handler);
      } else return Reflect.get(target,key);
    }
 }



var p = new Proxy(window, handler);
p.alert("alert message") // this works fine as I'm passing the correct context inside
                        // `method.apply`
p.location.valueOf(); // throws illegal invocation error as valueOf is a 
                      //non configurable property and I can't pass my custom "function(..args){}" function
                      //as I do for other methods

来源:https://stackoverflow.com/questions/51977354/illegal-invocation-during-non-configurable-method-call-with-javascript-proxies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!