-3

I have a simple function like this:

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

I would like to get as a result after calling

myFunction("myText", 3)

To output the following or similar:

{text:"myText",num:3}

What can be the code behind args_namesValues.

tit
  • 599
  • 3
  • 6
  • 25
  • `console.log({ text, num })`? – Andrew Li Dec 16 '18 at 19:13
  • Take a look at this post: https://stackoverflow.com/questions/4633125/is-it-possible-to-get-all-arguments-of-a-function-as-single-object-inside-that-f – jarodsmk Dec 16 '18 at 19:25
  • arguments will give only the values (seems also deprecated) – tit Dec 16 '18 at 19:32
  • What would your actual use case be? Not clear why you can define parameters but not use them later when they are already known – charlietfl Dec 16 '18 at 19:43
  • the reason is I want to transfer the function name and arguments (name and values) to the server for processing. – tit Dec 16 '18 at 20:13

3 Answers3

0
console.log({text, num});

Check ES6 property shorthand notation http://es6-features.org/#PropertyShorthand

Eftakhar
  • 455
  • 4
  • 17
0

My question was not clear... sorry I found a solution via Proxies, see http://2ality.com/2015/10/intercepting-method-calls.html

The following function allows to trace method calls/to hook it (thats what I wanted ...)

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);
}
tit
  • 599
  • 3
  • 6
  • 25
-1
console.log({text: text, num:num})

If you want to get parameter names dynamically you can check this post

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72