Although function literals create unnamed functions, the syntax allows a function name to be optionally
specified, which is useful when writing recursive functions that call themselves. For example:
var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); };
This line of code defines an unnamed function and stores a reference to it in the variable f . It does not
store a reference to the function into a variable named fact , but it does allow the body of the function to
refer to itself using that name. Note, however, that this type of named function literal was not properly
implemented before JavaScript 1.5.
本文介绍了JavaScript中如何使用命名函数表达式来实现递归调用。通过定义一个名为fact的函数并将其赋值给变量f,即使该函数本身未被命名为fact,但在函数内部仍可通过fact进行自我调用来实现阶乘计算。

被折叠的 条评论
为什么被折叠?



