我有一个简单的功能,像这样:
function myfunction(text: string,num: number) {
console.log( args_namesValues );
}
我想打电话后得到结果
myFunction("myText", 3)
要输出以下内容或类似内容:
{text:"myText",num:3}
args_namesValues 后面的代码是什么。
答案 0 :(得分:0)
console.log({text, num});
检查ES6属性速记符号 http://es6-features.org/#PropertyShorthand
答案 1 :(得分: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);
}
答案 2 :(得分:-1)
console.log({text: text, num:num})
如果要动态获取参数名称,可以检查this post