One, foreword

1. The browser passes two implicit arguments each time a function is called:

One is the function context object this, and the other is the array-like object arguments that encapsulates the arguments.

2. Unlike other programming languages, ECMAScript does not verify that the number of arguments passed to a function equals the number of arguments defined by the function. Any developer-defined function can take any number of arguments (but up to 255, according to Netscape’s documentation) without raising any errors. Any missing arguments are passed to the function as undefined, and redundant functions are ignored.

That is, parameters are matched from left to right. If the number of arguments is less than the parameter, the following parameters are assigned the value of undefined. Such as:

function fn (a, b, c) {
    console.log(a, b, c); // 1 2 undefined
    // The length attribute of a function object is the number of its parameters
    console.log(fn.length); / / 3
}
fn(1.2);
Copy the code

Second, the arguments

1, describe,

Arguments are an object, but arguments are used to store arguments in order. They have the same access properties and methods as arrays and have the array length property. Arguments are a special object. We know we say arguments when we hear class arrays.

That is, arguments is an array-like object that stores the arguments actually passed to the function, so that a function is called beyond the list of arguments defined by the function declaration.

function fn() {
    console.log(arguments);
    console.log(typeof arguments); // object
    console.log(toString.call(arguments)); // [object Arguments]
}
fn('name'.'age');
Copy the code

2. Access arguments and check the number of arguments

Arguments access individual arguments in the same way as they access array elements. Arguments [0], arguments[1], arguments[n] can be accessed without specifying argument names in functions. The length attribute tells you the number of arguments.

function f2() {
    console.log(arguments[0]); // name
    console.log(arguments[1]); // age
    console.log(arguments.length); / / 2
}
f2('name'.'age');
Copy the code

3. Callee attributes

Each object has its own property, and Arguments has a callee property that returns the Function object being executed.

function f3() {
    console.log(arguments.callee === f3); // true
}
f3('name'.'age');
Copy the code

4. Arguments can be modified

In normal mode, the Arguments object is allowed to be modified at run time.

function f4() {
    arguments[0] = 'sex';
    console.log(arguments[0]); // sex
}
f4('name'.'age');
Copy the code

5. Convert to a real array

Arguments is an array-like object with no Array attributes other than the length attribute and index element. For example, it has no POP method. But it can be converted to a real Array:

function f5(){
    // You can use slice to convert arguments to real arrays
    var args1 = Array.prototype.slice.call(arguments);
    var args2 = [].slice.call(arguments);
    You can also use the array.from () method or the extension operator to convert arguments to a real Array
    var args3 = Array.from(arguments);
    var args4 = [...arguments];
}
f5('name'.'age');
Copy the code

Three, the application

1, usearguments.lengthTo see if the number of arguments and parameters is the same

function fn (a, b, c) {
    if(fn.length ! =arguments.length) {
        console.log('Inconsistent number of parameters and arguments');
    } else{
        console.log('Same number of parameters as arguments');
    }
}
fn(1.2);
Copy the code

2, to borrowarguments.calleeTo make anonymous functions recurse:

let sum = function (n) {
    if (n == 1) {
        return 1;
    } else {
        return n + arguments.callee(n - 1); // 5, 4, 3, 2, 1}}console.log(sum(6)); / / 21
Copy the code

3. Iterate over the parameter summation or maximization

function max () {
    var max = arguments[0];
    for (item of arguments) {
        if(item > max) { max = item; }}return max;
}
console.log(max(5.3.2.9.4)); / / 9
Copy the code

4. Simulate function overloading

An overloaded function is a special case of a function. For ease of use, C++ allows several functions of the same name to be declared in the same scope, but the formal parameters (the number, type, or order of the parameters) of these functions must be different, that is, the same function to perform different functions.

Function overloading can be simulated by using arguments to determine the number of arguments passed to the function:

function doAdd() {
    if(arguments.length == 1) {
        console.log(arguments[0] + 5);
    } else if(arguments.length == 2) {
        console.log(arguments[0] + arguments[1]);
    }
}
doAdd(10);  / / 15
doAdd(10.20); / / 30
Copy the code

Four,

Arguments is an array object used to store arguments. With length, Callee and other attributes; Arguments can be accessed as arguments[0]; Can be converted to a real array.

Arguments are associated with functions, which are only available at function execution time and cannot be explicitly created.

Arguments can be used to iterate over arguments; Through Callee to achieve recursion; Function overloading can also be simulated.