美文网首页
arguments.callee的用法

arguments.callee的用法

作者: Iterate | 来源:发表于2020-11-29 01:04 被阅读0次

argument为函数内部对象,包含传入函数的所有参数,arguments.callee代表函数名,多用于递归调用,防止函数执行与函数名紧紧耦合的现象,对于没有函数名的匿名函数也非常起作用。举例如下:

function factorial(num){
      if(num<=1){
          return 1;
      }else{
          return num*arguments.callee(num-1);  //arguments.callee代表factorial
      }
  }
  var trueFactorial = factorial;
  factorial = function(){
      return 0;
  }
   alert(trueFactorial(5)); //结果为120,因为js中函数没有重载,所以如果递归调用时使用函数名,则执行最后一个该函数名的函数,即返回0
   alert(factorial(5));//结果为0

匿名函数的递归:

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

相关文章

  • arguments.callee用法  

    arguments.callee用法 arguments.callee 在哪一个函数中运行,它就代表哪个函数。 一...

  • arguments.callee用法

    arguments.callee 在哪一个函数中运行,它就代表哪一个函数。 一般用在匿名函数中。 在匿名函数中有时...

  • arguments.callee用法

    argument.callee 在那个函数中运行,他就代表哪一个函数。一般用在匿名函数中。在匿名函数中有时候需要自...

  • arguments.callee的用法

    argument为函数内部对象,包含传入函数的所有参数,arguments.callee代表函数名,多用于递归调用...

  • 常用但易忘的一些知识点

    递归调用arguments.callee(); caller和callee:arguments.callee返回当...

  • arguments.callee

    arguments.callee 属性包含当前正在执行的函数。 arguments.callee 从ES5严格模式...

  • JS自身函数调用

    使用arguments.callee 不使用arguments.callee的情况下 给函数添加个名字,并进行调用

  • arguments.callee 的使用

    先阅读以下递归代码 通过arguments.callee代替函数名 来看看打印出arguments.callee是...

  • 14.arguments的callee问题和caller

    function test(){ console.log(arguments.callee == test) ...

  • JS 日常一些记录

    - arguments.callee 从ES5严格模式中删除

网友评论

      本文标题:arguments.callee的用法

      本文链接:https://www.haomeiwen.com/subject/otxcwktx.html