[英]javascript - using Proxy trap when instantiating an object
I would like to have a javascript function that I can instantiate, and catch every undefined method that is being called to it (Proxy Trap). 我想要一个可以实例化的javascript函数,并捕获正在调用它的每个未定义方法(代理陷阱)。
What I have so far is this: 我到目前为止所拥有的是:
var MyProxyFunction = new Proxy(function () {
console.log(">>>>>>>>>>>>>>>>>>>> constructor");
}, {
get: function(target, prop) {
if (target[prop] === undefined) {
return function() {
console.log('an otherwise undefined function!!');
};
}
else {
return target[prop];
}
}
});
Now, if I call MyProxyFunction.foo()
, it will get called (I will see the "constructor" firing off and the log from the get function). 现在,如果我调用
MyProxyFunction.foo()
,它将被调用(我将看到“构造函数”启动并从get函数获取日志)。
But what I would like to do is have this object instantiated (and do some initialization in the constructor) like this: 但是我想做的是像下面这样实例化该对象(并在构造函数中进行一些初始化):
var myObj = new MyProxyFunction();
myObj.foo();
But when I do that than I get that foo()
is not a function. 但是当我这样做时,我得到
foo()
不是一个函数。 Why? 为什么? and how can I make it work when instantiating the proxy?
实例化代理时如何使它工作?
The explanation for that behaviour is that your constructor is proxied, but not the objects it constructs. 对此行为的解释是,您的构造函数被代理了,但没有构造它的对象。 So when you write
new MyProxyFunction
the proxied constructor is called, but the constructor creates a new object which has nothing to do with Proxy
, but with the prototype
property of the constructor. 因此,当您编写
new MyProxyFunction
时,将new MyProxyFunction
代理的构造函数,但是构造函数会创建一个新对象,该对象与Proxy
无关,但与构造函数的prototype
属性无关。
There are several ways to make it work. 有几种方法可以使其工作。
function MyProxyFunction() { console.log(">>>>>>>>>>>>>>>>>>>> constructor"); }; MyProxyFunction.prototype = new Proxy(MyProxyFunction.prototype, { get: function(target, prop) { if (target[prop] === undefined) { return function() { console.log('an otherwise undefined function!!'); }; } else { return target[prop]; } } }); var myObj = new MyProxyFunction(); myObj.foo(); console.log('constructor:', myObj.constructor.name); console.log('Is instance of MyProxyFunction?: ', myObj instanceof MyProxyFunction);
It now looks a bit strange to use the name MyProxyFunction , as it is not the function (the constructor) itself that is proxied. 现在使用名称MyProxyFunction看起来有点奇怪 ,因为不是代理的函数(构造函数)本身。
If you want to have a constructor that creates a new proxy each time you instantiate an object with it, then don't assign new Proxy
directly to MyProxyFunction
, but make it a plain constructor function that returns a new Proxy
. 如果想让构造函数每次用它实例化一个对象时都创建一个新的代理,则不
MyProxyFunction
new Proxy
直接分配给MyProxyFunction
,而应使其成为返回一个new Proxy
的普通构造函数。
The object you then have to proxy is this
. 然后,您必须代理的对象是
this
。
function MyProxyFunction() { console.log(">>>>>>>>>>>>>>>>>>>> constructor"); return new Proxy(this, { get: function(target, prop) { if (target[prop] === undefined) { return function() { console.log('an otherwise undefined function!!'); }; } else { return target[prop]; } } }); } var myObj = new MyProxyFunction(); myObj.foo(); console.log('constructor:', myObj.constructor.name); console.log('Is instance of MyProxyFunction?: ', myObj instanceof MyProxyFunction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.