简体   繁体   中英

Result of eval( arguments.callee );

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM