Reputation: 123
I just ran the below function in Chrome Developer Tool, but nothing happened. So what is happening - is this going to be an infinite loop?
function foo() {
eval( arguments.callee );
}
foo( );
If I modify the function to below:
function foo() {
console.log('Called');
eval( arguments.callee.toString() );
}
foo();
Output:
Called
Output is only printed one time, so whats happening?
Upvotes: 1
Views: 56
Reputation: 1074575
eval(arguments.callee)
converts arguments.callee
to string, which will look very much like your function declaration, and then evaluates that string — which does not run it, it just evaluates the function declaration, creating a function.
Your toString
version just does the first part explicitly.
If you did eval(arguments.callee)()
(note the ()
at the end), that would call it (sort of recursively, technically different functions are created, but...) and eventually lead to a stack overflow error.
Note that arguments.callee
is disallowed in strict mode. If you need to refer to the function being called, give it a name and use the name.
Upvotes: 5