• js arguments


    1. js在处理函数的参数的时候,解释器传递给函数的是一个类似于数组的东西:arguments,有length可通过下标访问 

     function add(){
        var sum = 0;
        for(var i = 0, len = arguments.length; i < len; i++){
          sum = sum + int(arguments[i]);
        }
      return sum;
      }


    2. arguments的callee属性:
      (1).callee属性是用来引用当前正在执行的函数,可以用来实现递归。    

        function add(x){
          if(x == 1){
            return 1;
          } else {
            return n + arguments.callee(n-1);
          }
        }

         (2).匿名参数    

             var result = function(x) {
          if(x == 1){
            return 1;
          } else {
            return n + arguments.callee(n-1);
          }
        }

    3.方法重载    

             function test(){ 
          if(arguments.length==1){ 
            alert(arguments[0]); 
          } else if (arguments.length==2){ 
            alert(arguments[0]+arguments[1]); 
          }
        }
        test(2);//2 
        test(1,2);//3
  • 相关阅读:
    191. Number of 1 Bits
    190. Reverse Bits
    532. K-diff Pairs in an Array
    485. Max Consecutive Ones
    236. Lowest Common Ancestor of a Binary Tree
    235. Lowest Common Ancestor of a Binary Search Tree
    面试题68:树中两个节点的最低公共祖先
    Java—方法重写
    Java—继承
    代码块(Java)
  • 原文地址:https://www.cnblogs.com/lindsayzhao103011/p/3190499.html
Copyright © 2020-2023  润新知