What mechanism allows a JavaScript function to refer to arguments to its caller by the name of the calling function, and why is it still in the language?
I was looking for tail call optimization (or rather lack thereof) in V8 and came across this post ( https://code.google.com/p/v8/issues/detail?id=457 )
Eric Corrie's example is given below.
function foo(x) { return bar(x + 1); } function bar(x) { return foo.arguments[0]; } foo(1)
At first, I thought that maybe calling the function sets its arguments field as some kind of weird global side effect, but it apparently lasts the entire time the call is made.
function foo(x) { return bar(x+1); } function bar(x) { return foo.arguments[0]; } console.log(foo(1));
Why is this behavior in language? Is it useful for anything other than backward compatibility?
EDIT:
I am not asking for the use of arguments to reference a pseudo-array of arguments in the current function body, for example
function variadic() { for (var i = 0; i < arguments.length; i++) { console.log("%s-th item is %s", i, JSON.stringify(arguments[i])); } } variadic(1, 2, 3, 4, 5, []);
I ask for the use of somefunction.arguments to reference the arguments caller.
source share