Arguments object properties

The Arguments object is a built-in local variable that all functions have, representing the arguments that the function actually receives, and is an array structure of classes.

Arguments is an array of classes because it has no other array properties except the length property.

1. The function cannot be accessed from outside

Arguments objects can only be used inside functions; arguments objects cannot be accessed outside functions. While arguments objects exist in function scope, one function cannot directly access the arguments of another function.

Arrow functions don’t have “arguments”

If we call Arguments in an arrow function, the arguments we call do not belong to the arrow function, but to a “normal” function outside the arrow function.

    console.log(typeof arguments); // undefined
    function fn () {
      console.log(arguments.length); / / 4
    }
    fn (1.2.3.4)
Copy the code

2. Accessible by index

The Arguments object is a array-like structure that can be accessed via an index. Each item represents the value of the corresponding argument passed, and undefined is returned if the argument index does not exist.

    function fn (num1, num2) {
      console.log(arguments[0]); / / 1
      console.log(arguments[1]); / / 2
      console.log(arguments[2]); // undefined
    }
    fn (1.2)
Copy the code

3. It depends on the argument

Arguments object values are determined by arguments, not by defined parameters, and the parameters take up separate memory space. The relationship between arguments objects and parameters can be summarized as follows.

1. The length property of the arguments object is defined at function call time and does not change with Korean processing.

The arguments object has the same value as the parameter, and can change with each other.

The arguments object returns undefined if no arguments are passed.

The arguments object cannot change the values of a given parameter without passing an argument.

    function fn (a, b, c) {
      console.log(arguments.length); / / 2

      arguments[0] = 11;
      console.log(a); / / 11

      b = 12;
      console.log(arguments[1]); / / 12

      arguments[2] = 3;
      console.log(c); // undefined

      c = 13;
      console.log(arguments[2]); / / 3
      
      console.log(arguments.length); / / 2
    }
    fn (1.2)
Copy the code

The above example nicely summarizes the nature of the Arguments object.

Arguments causes problems: parameters have no meaning

Let’s look at an example:

    function fn (a, b, c) {
      arguments[0] = 11;
      console.log(a); / / 11
      console.log(b); / / 2
    }
    fn (1.2)
Copy the code

Note: Never modify arguments objects directly

As in the example above, the arguments object has a very tenuous relationship to function parameters. Modifying the Arguments object risks losing the meaning of the function’s parameters. In strict mode, function arguments do not support changes to the Arguments object.

Arguments object application

Since arguments are fatally certain, how do we apply it? Let’s look at a couple of applications.

1. Determine the number of arguments

Define a function that explicitly requires that only three arguments be passed when called. If the number of arguments passed is not equal to three, an exception is thrown.

    function fn(a, b, c) {
      if (arguments.length ! = =3) {
        throw new Error("The number of arguments passed is not 3.")}// do something
    }
Copy the code

2, any number of parameter processing

Define a function that deals specifically with the first few arguments passed, and with the rest no matter how many arguments are passed.

For example, defining a function that concatenates multiple strings using delimiters and returns a result string. The first argument represents the delimiter and all the following arguments represent the concatenated characters.

    function joinStr (seperator) {
      // Arguments is an array of classes, and arguments cannot be manipulated directly
      The slice() function can be indirectly called via the call() function to return a new array and manipulate it
      let str = Array.prototype.slice.call(arguments.1)
      return str.join(seperator)
    }

    joinStr(The '-'.'x'.'y'.'z') // x-y-z
    joinStr(', '.'o'.'p'.'q') // o,p,q
Copy the code

3. Simulate function overloading

Function overloading is a program that has the same named functions and uses arguments to call different functions.

Javascript does not allow function overloading because the same named function will be overwritten by the next one. We can use arguments to simulate function overloading.

Define a function that implements a multi-valued sum.

    function sum () {
      let arr = [...arguments]
      return arr.reduce((pre, cur) = > {
        return pre + cur
      }, 0)}console.log(sum(1.2.3)); / / 6
Copy the code