Search code examples
javascriptecmascript-6ecmascript-5

How do I return argument(s) as string in javascript?


I'm trying to make a function which can return argument(s) when it's called.

example( fooObject.foo[1] );
//return "fooObject.foo[1]"

example( fooFunc("foo",42) );
//return "fooFunc(\"foo\",42)"

like this.

I found out that I can get each arguments by using "arguments" method in the function, but cannot find the way to make the argument into the string.


Solution

  • This is not possible. A function is called with the values of its arguments, not the expressions that formed them. There is no way to access the code of the expressions as strings.