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去代替函数名,就可以确保函数不管怎么调用都不会出错

  • 相关阅读:
    SharedPreferences 使用
    activity在activity上面
    组合组件
    浏览器的渲染原理
    Node 入门<1>
    css 样式优先级
    z-index
    事件代理
    XSS && CRLF && property&attribute
    webpack 学习笔记
  • 原文地址:https://www.cnblogs.com/lwwen/p/5593375.html
Copyright © 2011-2022 走看看