• JavaScript中的arguments详解


    1. arguments

    • arguments不是真正的数组,它是一个实参对象,每个实参对象都包含以数字为索引的一组元素以及length属性。

        (function () {
            console.log(arguments instanceof Array); // false  
            console.log(typeof (arguments)); // object  
        })();
      
    • 只有函数被调用时,arguments对象才会创建,未调用时其值为null

        console.log(new Function().arguments);  // null
      
    • JavaScript函数调用不对参数做任何检查。

    • 当调用函数的时候传入的实参比形参个数少时,剩下的形参都被设置为undefined。

    给被省略的参数赋值:

    function getPropertyNames(o, /* optional */ a) {
        a = a || []; // 等于 if(a == undefined) a = [];
        for (var property in o) {
            a.push(property);
        }
        return a;
    }
    console.log(getPropertyNames({ '1': 'a', '2': 'b' })); // ["1", "2"]
    console.log(getPropertyNames({ '1': 'a', '2': 'b' }, ['3'])); // ["3", "1", "2"]
    
    • 当调用函数传入的参数比形参个数多时,通过标识符arguments可以获得未命名的引用。

    验证实参的个数

    function f(x, y, z) {
        // 首先,验证传入实参的个数是否正确
        if (arguments.length != 3) {
            throw new Error("function f called with " + arguments.length + "arguments, but it expects 3 arguments.");
        }
        // 再执行函数的其他逻辑
    }
    f(1, 2); // Uncaught Error: function f called with 2arguments, but it expects 3 arguments.
    

    查看实参和形参的个数是否一致

    // 查看实参和形参的个数是否一致
    function add(a, b) {
        var realLen = arguments.length;
        console.log("realLen:", arguments.length);  // realLen: 5
        var len = add.length;
        console.log("len:", add.length);    // len: 2
        if (realLen == len) {
            console.log('实参和形参个数一致');
        } else {
            console.log('实参和形参个数不一致');
        }
    };
    add(1, 2, 3, 6, 8);    
    

    模拟函数重载

    function doAdd() {
        if (arguments.length == 1) {
            console.log(arguments[0] + 5);
        } else if (arguments.length == 2) {
            console.log(arguments[0] + arguments[1]);
        }
    }
    

    接受任意数量的实参,并返回其中的最大值

    function max(/* ... */) {
        var max = Number.NEGATIVE_INFINITY;
        // 遍历实参,查找并记住最大值
        for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] > max) {
                max = arguments[i];
            }
        }
        // 返回最大值
        return max;
    }
    console.log(max(1, 10, 110, 2, 3, 5, 10000, 100));  // 10000
    

    自动更新

    function f(x) {
        console.log(x); // 1,输出实参的初始值
        arguments[0] = null;  // 修改实参数组的元素同样会修改x的值
        console.log(x); // null
        x = 'a';    // 修改x的值同样会修改实参数组的元素
        console.log(arguments[0]);  // a
    }
    f(1);
    
    • 实参对象定义了callee和caller属性,callee属性指代正在执行的函数,caller指代调用当前正在执行的函数的函数
    • arguments 的完整语法如下:
      [function.]arguments[n]
      function :当前正在执行的 Function 对象的名字。
      n :要传递给 Function 对象的从0开始的参数值索引。

    查看callee和caller属性

    function a() {
        console.log('a...');
        b();
    }
    
    function b() {
        console.log('b...');
        console.log(b.arguments.callee);
        console.log(b.caller);
    }
    a();
    

    通过callee实现递归调用自身

    var factorial = function (x) {
        if (x <= 1) { return 1; }
        return x * arguments.callee(x - 1);
    }
    console.log(factorial(5));  // 120
    

    参考资料

  • 相关阅读:
    4.12 活跃性
    4.13 ReentrantLock
    异步模式之生产者消费者
    同步模式之保护性暂停Guarded Suspension
    android 学习
    android 学习
    android 学习
    android 学习
    android 学习
    android 学习(开课博客)
  • 原文地址:https://www.cnblogs.com/huangwenjie/p/9214112.html
Copyright © 2020-2023  润新知