-3

我有一个像这样的简单功能:

function myfunction(text: string,num: number) {
   console.log( args_namesValues );
}

我想在打电话后得到结果

myFunction("myText", 3)

输出以下或类似内容:

{text:"myText",num:3}

args_namesValues背后的代码是什么。

4

3 回答 3

0

我的问题不清楚......对不起,我通过代理找到了解决方案,请参阅http://2ality.com/2015/10/intercepting-method-calls.html

以下函数允许跟踪方法调用/挂钩它(这就是我想要的......)

function traceMethodCalls(obj) {
    let handler = {
        get(target, propKey, receiver) {
            const origMethod = target[propKey];
            return function (...args) {
                let result = origMethod.apply(this, args);
                console.log(propKey + JSON.stringify(args)
                    + ' -> ' + JSON.stringify(result));
                return result;
            };
        }
    };
    return new Proxy(obj, handler);
}
于 2018-12-17T02:22:54.443 回答
0
console.log({text, num});

检查 ES6 属性简写符号 http://es6-features.org/#PropertyShorthand

于 2018-12-16T19:27:49.893 回答
-1
console.log({text: text, num:num})

如果您想动态获取参数名称,可以查看这篇文章

于 2018-12-16T19:15:40.893 回答