简体   繁体   English

javascript-实例化对象时使用代理陷阱

[英]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. 有几种方法可以使其工作。

1. Apply the proxy on the prototype object 1.将代理应用于原型对象

 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看起来有点奇怪 ,因为不是代理的函数(构造函数)本身。

2. Create a proxy for each instance 2.为每个实例创建一个代理

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.

相关问题 Javascript:在代理中陷阱“ in”运算符 - Javascript: Trap “in” operator in proxy 使用代理 object 时如何将 arguments 捕获到目标方法? - How do I trap arguments to a target method when using a Proxy object? 实例化对象时参考Javascript对象方法 - Reference Javascript object method when instantiating object 实例化JavaScript对象时的最佳做法是什么? - What best practice when instantiating a JavaScript object? 无法将“应用”陷阱设置为代理对象 - Can't set "apply" trap to Proxy object 枚举()的替代方法是JavaScript代理陷阱... in - What is the alternative for enumerate() for JavaScript Proxy to trap for … in ES6 代理:set() 陷阱不触发,当在目标对象的方法中设置时 - ES6 Proxy: set() trap not triggering, when setting inside target object's method 无法使用代理捕获 customElements 上的访问器调用? - Unable to trap accessor calls on customElements using Proxy? javascript Proxy of function apply trap gives no access to the receiver Proxy - javascript Proxy of function apply trap gives no access to the receiver Proxy 是否可以使用代理JavaScript对象通过清空对象优先级以获取信息/数据来“诱捕”用户查找其属性/值? - Is it possible to use a Proxy JavaScript Object to 'trap' a user from finding its properties/values by emptying the Object PRIOR to getting info/data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM