简体   繁体   English

eval(arguments.callee);的结果

[英]Result of eval( arguments.callee );

I just ran the below function in Chrome Developer Tool, but nothing happened. 我只是在Chrome开发者工具中运行了以下功能,但没有任何反应。 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. eval(arguments.callee)arguments.callee转换为字符串,该字符串非常类似于您的函数声明,然后评估该字符串- 不会运行它,它只会评估函数声明,创建一个函数。

Your toString version just does the first part explicitly. 您的toString版本仅明确执行第一部分。

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. 如果你做eval(arguments.callee)()注意()在结束), 将要求它( 排序的递归,技术上不同的功能创建的,但是......),并最终导致堆栈溢出错误。


Note that arguments.callee is disallowed in strict mode. 请注意,严格模式下不允许arguments.callee If you need to refer to the function being called, give it a name and use the name. 如果需要引用被调用的函数,请给它命名并使用该名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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