使用代理对象时如何将参数捕获到目标方法? [英] How do I trap arguments to a target method when using a Proxy object?

查看:26
本文介绍了使用代理对象时如何将参数捕获到目标方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Javascript 代理 对象 来捕获传递给我正在代理的目标的方法"的参数.

I'm trying to use Javascript Proxy objects to trap the arguments that are passed to a 'method' of the target that I'm proxying.

请考虑这个例子:

var test = {
    doSomething: function() {
        console.log( arguments.length );
    }
};

var testProxy = new Proxy( test, {
    get: function( target, property, receiver ) {

        // I'd like to have access to any arguments when
        // the property being accessed here is a function
        // that is being called

        return target[ property ];
    }
} );

testProxy.doSomething( 'this', 'is', 'lame' ); // I want to trap those arguments

当属性实际上是一个函数时,这些 Proxy 对象似乎只允许您捕获访问属性,而不是捕获实际函数调用及其参数.

It appears that these Proxy objects only allow you to trap accessing the property, but not the actual function call, with its arguments, when the property is in fact a function.

在对此事进行一些反思后,我得到"(原谅双关语)get 方法仅用于属性访问,而不是调用,但是我本来希望也可以在 Proxy 中定义类似 call 方法的东西.

After reflecting a bit on the matter, I "get" (pardon the pun) that the get method is just intended for property access, in stead of invocation, but then I would have expected to be able to define something like a call method in the Proxy as well.

也许在 Proxy 中定义一个 apply 方法是可行的,但是我可能不得不为每个对象创建一个 Proxy 对象我要代理的对象的单个方法;这不是我所追求的.

Perhaps it's doable with defining an apply method in the Proxy, but then I'd probably have to create a Proxy object for each individual method of the object I want to proxy; and that's not what I am after.

除非我在这里忽略了一种实际的替代可能性:如何在 Proxy 实现中忽略了这一点?!代理的全部意义不就是能够拦截方法调用及其参数吗?

Unless I'm overlooking an actual alternative possibility here: how is it that this is overlooked in the Proxy implementation?! Isn't the whole point of a proxy to be able to intercept method calls and their arguments as well?

或者这是对 Javascript 的另一个误解,就我而言,关于 Javascript 不是经典的"OOP 语言,而且我正在寻找的功能在 Javascript 的上下文中实际上没有意义?

Or is this yet another misunderstanding of Javascript, on my part, about Javascript not being a 'classical' OOP language, and that the functionality I'm looking for wouldn't actually make sense in the context of Javascript?

推荐答案

实际上 有一种方法可以做到这一点,当然!只是我想得不够彻底.我可以只返回一个代理"函数并将参数捕获在那里:

There actually is a way to do this, of course! I just hadn't thought it through thoroughly enough. I can just return a 'proxy' function and trap the arguments in there:

var test = {
    doSomething: function() {
        console.log( arguments.length );
    }
};

var testProxy = new Proxy( test, {
    get: function( target, property, receiver ) {

        switch( property ) {
            case 'doSomething':
              // you just have to return a proxy function
              return function() {
                  // arguments accessible, after all!
                  console.log( 'testProxy::doSomething() arguments.length: ' + arguments.length );

                  // here you can still invoke the original method, of course
                  target[ property ].apply( this, arguments );
              }
            break
        }

        return target[ property ];
    }
} );

testProxy.doSomething( 'this', 'is', 'not', 'so', 'lame', 'after', 'all' );

这篇关于使用代理对象时如何将参数捕获到目标方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆