zoukankan      html  css  js  c++  java
  • 递归函数

    递归函数:

    function factorical(num){

      if(num<=1){

        return 1;

      }

      else{

        return num*factorical(num-1);
      }

    }

    factorial(2)//2

    这个递归函数就是用函数来调用函数本身,但是这样真的好吗,好 接下来看这里

    var another=factorical;

    factorical=null;

    console.log(another(2))//会报错说 factorical not a function

    这就是函数调用函数的弊端,那怎么解决呢,看下面

    function factorical(num){

      if(num<=1){

        return 1;

      }

      else{

        return num*arguments.callee(num-1);
      }

    }

    var another=factorical;

    factorical=null;

    console.log(another(2))//2

    以上 用arguments.callee去代替函数名,就可以确保函数不管怎么调用都不会出错

  • 相关阅读:
    多条件复合搜索的实现
    mysql字符集统一
    JS控制彈出窗口
    mysql常用sql
    正则表达式
    航班时间
    1月19日
    1月28日check小爱用
    在么小猫
    大连美发备考
  • 原文地址:https://www.cnblogs.com/lwwen/p/5593375.html
Copyright © 2011-2022 走看看