Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 代理使用会弄乱function.caller.name_Javascript_Oop_Proxy Classes - Fatal编程技术网

Javascript 代理使用会弄乱function.caller.name

Javascript 代理使用会弄乱function.caller.name,javascript,oop,proxy-classes,Javascript,Oop,Proxy Classes,我试图使用JavaScript模拟一个更像类的继承模型,但当我试图将其与JavaScript代理的思想结合起来时,遇到了一个问题 长话短说,在我的类类型的定义中,我有一个函数\u super(),其语义是“当子类B实例上的方法X调用_super(),调用父类a上的方法X时”: 我依靠function.caller.name方法获取调用方法的名称(在我们的示例中为“X”)。然后我在父类上调用它 const Class = { ... _super: function _super(...a

我试图使用JavaScript模拟一个更像类的继承模型,但当我试图将其与JavaScript代理的思想结合起来时,遇到了一个问题

长话短说,在我的类类型的定义中,我有一个函数\u super(),其语义是“当子类B实例上的方法X调用_super(),调用父类a上的方法X时”:

我依靠function.caller.name方法获取调用方法的名称(在我们的示例中为“X”)。然后我在父类上调用它

const Class = {
...
    _super: function _super(...args) {
      // Get a handle on the function in which this function is invoked:
      const callerMethod = _super.caller.name;
      ...
    },
...
};
这是正确的。当我在类构造的顶部添加代理对象时,问题就开始了(我想捕获一些方法调用)

现在,_super()方法中的function.caller是代理处理程序对象中的匿名函数(显然是…),这会打乱程序流程


我的问题是:有没有办法避免这种情况?或者换个角度思考?或者我必须完全放弃*.caller.name方法吗?

我想到的唯一一件事就是检查堆栈,找到第一件不是“超级”的东西。我觉得这很傻,但事情是这样的

const类={
_super:function\u super(…args){
让被叫的方法;
设s=(新错误)
.stack.split(“\n”)
.切片(2);
while(s.length&&s[0]。包括(“超级”))
s、 移位();
设m=(s[0]| |').match(/^\s*at\s\w+\(\w+/);
callerMethod=m?m[1]:空;
console.log('super call[%s]',callerMethod)
},
foo:function(){
这个._super()
}
};
函数跟踪方法调用(obj){
常量处理程序={
获取(目标、道具键、接收器){
const origMethod=目标[propKey];
设f={
[propKey]:函数(…参数){
console.log('tracing',propKey)
原始方法绑定(此)()
}
};
返回f[propKey];
},
};
返回新代理(obj,handler);
}
obj=对象。创建(类)
obj.foo()
跟踪=跟踪方法调用(obj)

tracked.foo()
谢谢!是的,看来我得重新考虑我的方法了。
const Class = {
...
    _super: function _super(...args) {
      // Get a handle on the function in which this function is invoked:
      const callerMethod = _super.caller.name;
      ...
    },
...
};
function traceMethodCalls(obj) {
  const handler = {
    get(target, propKey, receiver) {
      const origMethod = target[propKey];
      return function (...args) {
        // Do stuff
      };
    },
  };
  return new Proxy(obj, handler);
}