函数声明提前

原创 1244321641 随笔 总结日志 92阅读 2018-01-17 17:50:37 举报

function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
var trueFactorial = factorial;
alert(trueFactorial(5)); //120

    factorial = function() {
        return 0;
    }                
    alert(trueFactorial(5));// 120 如果没有使用arguments.callee,将返回0

关键看 var trueFactorial = factorial;的位置,而 factorial = function() {return 0;}的 函数无法提前,调用只能在函数之后调用

var factorial = function() {
return 0;
}
var trueFactorial = factorial;
alert(trueFactorial(5)); //0

function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
alert(trueFactorial(5));// 0

    var trueFactorial = factorial;
    alert(trueFactorial(5));    //120  
    var factorial = function() {
        return 0;
    }  

function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
alert(trueFactorial(5));// 120

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复