https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee#A_use_of_arguments.callee_with_no_good_alternative
简要地总结是去掉它几乎不会影响功能的实现,并且性能还能得到提升。
它最广泛的用途就是在匿名函数里递归调用自身,但 EcmaScript 3 已允许有名字的函数表达式,且不会污染命名空间,所以在实现同样的功能时并不会变得不优雅。
而访问 arguments 是个很昂贵的操作,因为它是个很大的对象,每次递归调用时都需要重新创建(虽然 arguments.callee 是不变的,但是 arguments.caller 是不一样的,调用的参数也是不一样的)。由于 arguments 是可变的,就不能被尾递归和内联优化了。
"However, this was actually a really bad solution as this (in conjunction with other
MDN 中提到有种使用方式是无可替代的:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee#A_use_of_arguments.callee_with_no_good_alternative。
不过 eval code 和 Function code 在实践中也是不提倡的。
它最广泛的用途就是在匿名函数里递归调用自身,但 EcmaScript 3 已允许有名字的函数表达式,且不会污染命名空间,所以在实现同样的功能时并不会变得不优雅。
而访问 arguments 是个很昂贵的操作,因为它是个很大的对象,每次递归调用时都需要重新创建(虽然 arguments.callee 是不变的,但是 arguments.caller 是不一样的,调用的参数也是不一样的)。由于 arguments 是可变的,就不能被尾递归和内联优化了。
"However, this was actually a really bad solution as this (in conjunction with other
arguments, callee, and caller issues) make inlining and tail recursion impossible in the general case (you can achieve it in select cases through tracing, etc., but even the best code is suboptimal due to checks that would not otherwise be necessary.) The other major issue is that the recursive call will get a different this value, "MDN 中提到有种使用方式是无可替代的:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee#A_use_of_arguments.callee_with_no_good_alternative。
不过 eval code 和 Function code 在实践中也是不提倡的。
作者:孙竟
链接:https://www.zhihu.com/question/20611911/answer/19023033
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
浙公网安备 33010602011771号