一、函数名调用
function factorial (num) {
if (num <= 1){
return 1
} else {
return num * factorial(num-1)
}
}
const anotherFactorial = factorial
factorial = null
alert(anotherFactorial(4))//Uncaught TypeError: factorial is not a function
二、arguments.callee(指向正在执行的函数)
使用callee代替函数名,可实现函数递归的同时解决原函数发生变化的情况
function factorial (num) {
if (num <= 1){
return 1
} else {
return num * arguments.callee(num-1)
}
}
const anotherFactorial = factorial
factorial = null
alert(anotherFactorial(4))//24
但在严格模式下无法正常使用:
function factorial (num) {
// 'use strict';
if (num <= 1){
return 1
} else {
return num * arguments.callee(num-1)
}
}
const anotherFactorial = factorial
factorial = null
alert(anotherFactorial(4))//Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
三、函数命名表达式
let factorial = (function f (num) {
'use strict';
if (num <= 1){
return 1
} else {
return num * f(num-1)
}
})
const anotherFactorial = factorial
factorial = null
alert(anotherFactorial(4))//24