//使用递归函数实现阶乘问题
function factorial(num){
if(num<=1){
return 1;
}else{
return num*factorial(num-1)
}
}
console.log(factorial(5));//120
//在函数有名字,而且名字以后也不会变的情况下的情况下,这样定义没有问题。
这样写的第一个问题是:
var anotherFactorial = factorial;
factorial = null;
console.log(anotherFactorial(5));//Uncaught TypeError: factorial is not a function
以上代码先把 factorial() 代码保存在变量 anotherFactorial 中,然后将 factorial() 设置为 null,结果指向原始函数的引用只剩下一个了。但在接下来调用 anotherFactorial() 时,由于必须执行 factorial(), 而 factorial 已不再是函数。所以会导致错误。
另一个问题是:这个函数的执行与函数名 factorial 紧紧的耦合在了一起。
针对这两个问题,我们可以使用 arguments.callee 来解决
function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
console.log(factorial(5));//120
var anotherFactorial = factorial;
factorial = null;
console.log(anotherFactorial(5));//120
在这个重写后的 factorial() 函数的函数体内,没有再引用 factorial。解决了耦合问题。
这样,无论引用函数时使用的是什么名字,都可以保证正常完成递归调用。
将 factorial() 设置为 null 后,调用 anotherFactorial() 时,也不会报错了。
但问题是:在严格模式下,不能通过脚本访问 arguments.callee,访问这个属性会导致错误。
"use strict"
function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
console.log(factorial(5));
//Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
使用命名函数表达式来解决严格模式下无法访问 arguments.callee 的问题
"use strict"
var factorial=(function f(num){
if(num<=1){
return 1;
}else{
return num*f(num-1)
}
});
console.log(factorial(5));//120
var anotherFactorial = factorial;
// factorial = null;
factorial = function() {
return 0;
}
console.log(anotherFactorial(5));//120
console.log(factorial(5));//返回0