callee属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>callee属性</title>
</head>
<body>
<script>
//阶乘函数。 3的阶乘 1*2*3
function factorial (num) {
if (num <= 1) {
return 1;
} else {
return num * factorial(num-1); //如果函数名修改了,那么此处也要修改
}
}
console.log(factorial(5));
//可以利用,函数里面的arguments的callee属性,减少与函数名的耦合。
function somename (num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num-1); //函数名随便改
}
}
console.log(somename(5));
</script>
</body>
</html>

浙公网安备 33010602011771号