1.最優秀的方式
var sum=(function(){
'use strict'
return function fun(num){
if(num<=1){
return 1;
}else{
return num+fun(num-1);
}
}
})()
console.log(sum(5));//15
var sumAnother=sum;
console.log(sumAnother(5));//15
sum=null;
console.log(sumAnother(5));//15
2,常規方式
function sum(num){
if(num<=1){
return 1;
}else{
return num+sum(num-1);
}
}
console.log(sum(5));//15
3,不提倡方式
function sum(num){
if(num<=1){
return 1;
}else{
return num+arguments.callee(num-1);
}
}
console.log(sum(5));//15
var sumAnother=sum;
console.log(sumAnother(5));//15
sum=null;
console.log(sumAnother(5));//15
複製代碼