• arguments对象,caller 和 callee


    arguments对象是比较特别的一个对象,arguments非常类似Array,但实际上又不是一个Array实例。

    它指的是函数对象里的参数,且只能在函数内部使用。

    使用

      检测函数的参数个数,引用属性 arguments.length。

      访问:arguments[0] 

    1.arguments对象的长度是由实参个数而不是形参个数决定的。

    function a(a,b,c){
      console.log(arguments.length)
      console.log(arguments[3])
      b=b-a;
      console.log(b)
    }
    a(1,2);//2 undefined 1

    2.JavaScript中函数是不能重载的。

    如何实现重载呢?

    function fn(){
                switch(arguments.length){
                    case 0:
                        //执行语句块
                        break;
                    case 1:
                        //执行语句块
                        break;
                    case 2:
                        //执行语句块
                        break;
                }
    }

    3.arguments对象中有一个非常有用的属性:callee。arguments.callee返回此arguments对象所在的当前函数引用。在使用函数递归调用时推荐使用arguments.callee代替函数名本身。

    function a(a){
    if(a==1){return 1}
    return a+arguments.callee(--a);
    }
    a(10);//55
    function ass(a,b,s){
        console.log(arguments.callee.length);
        console.log(arguments.length);
    }
    assl(1,2);//3 2

    arguments.length是实参长度,arguments.callee.length是形参长度

    caller 返回一个函数的引用,这个函数调用了当前的函数。

    使用这个属性要注意:

    1 这个属性只有当函数在执行时才有用
    2 如果在javascript程序中,函数是由顶层调用的,则返回null

    function b(){
        a()
    }
    function a(){
        alert(a===arguments.callee);//true
        alert(arguments.caller=b);//function b(){a()}
        alert(arguments.callee.caller===b);//true
    }
    b();
    var a = function() {   
        alert(a.caller);   //null
    }   
    a(); 

     var a = function() {
          alert(arguments.callee);
     }
     a();//var a = function() { alert(arguments.callee); }

     
  • 相关阅读:
    Leetcode 97 交错字符串 二维DP
    原生 js 实现图片裁剪
    Leetcode 735 行星碰撞
    Leetcode 283 移动零 双指针
    CMS收集器和G1收集器的区别
    十三.RTC时钟使用
    十三.I2C使用1——I2C基础和AP3216C的使用
    番外篇一——Ubuntu20.04和uboot之间nfs设置
    〇二——Uboot常用命令
    十二.UART串口通讯
  • 原文地址:https://www.cnblogs.com/mina-huojian66/p/6094474.html
Copyright © 2020-2023  润新知