标签:情况 数通 表达式 OLE 出错 null turn ons nts
// 函数声明 //关于函数声明,它的一个重要特征就是函数声明提升 fname() function fname() { // 函数体 console.log("hi"); } // 函数表达式 // fn();// 出错: fn is not a function var fn = function () { console.log("hello"); }
arguments.callee 是一个指向正在执行的函数的指针
/* 递归 */ // 实现一个经典的递归阶乘函数 function factorial(num) { if (num <= 1) { return 1; } else { // return num * factorial(num - 1); // 改进 知道,arguments.callee 是一个指向正在执行的函数的指针 return num * arguments.callee(num - 1) } } // 如果函数赋予另一个变量,再factorial = null 会出错 // 改进 知道,arguments.callee 是一个指向正在执行的函数的指针 var anotherFactorial = factorial; factorial = null; // console.log(anotherFactorial(4)); //出错! factorial is not a function console.log(anotherFactorial(4)); //24 /* 但在严格模式下,不能通过脚本访问arguments.callee,访问这个属性会导致错误 改进: */ var factorial2 = (function f(num) { if (num <= 1) { return 1; } else { return num * f(num - 1); } }); console.log(factorial2 instanceof Function);//true console.log(typeof(factorial2));//function var anotherFactorial2 = factorial2; factorial2 = null; console.log(anotherFactorial2(4));//24
以上代码创建了一个名为f()的命名函数表达式,然后将它赋值给变量factorial。即便把函数
赋值给了另一个变量,函数的名字f 仍然有效,所以递归调用照样能正确完成。
标签:情况 数通 表达式 OLE 出错 null turn ons nts
原文地址:https://www.cnblogs.com/chunying/p/13804042.html