Deep parsing of arguments objects in JavaScript

  • 2021-06-28 10:10:05
  • OfStack

arguments Definition

All functions have their own arguments object to store the parameters they actually receive, not just the list of parameters defined when the function is declared.It is not an array, but it is similar to an array. It has the same access properties and methods as Array 1. arguments [n] can access the values of the corresponding single parameter and has the array length property length.However, there are no methods for arrays.arguments can be converted to a real array through call and then manipulated.


var args = Array.prototype.slice.call(arguments);

Class Array

1. Determine if ARGUMENTS is an array


alert(arguments instanceof Array);
alert(arguments instanceof Object);

2. How to strictly judge that a data is an instance of an array (ARRAY) class


function isArray(value){
  if (typeof Array.isArray === "function") {
    return Array.isArray(value);
  }else{
    return Object.prototype.toString.call(value) === "[object Array]";
  }
}

3. Convert ARGUMENTS to Array
Method 1: Built-in types can find built-in attribute methods through prototype, which is Array.prototype.slice, the built-in method for accessing Array.Returns an array through the slice method.call is a method that calls one object to replace the current one with another.


var arg = Array.prototype.slice.call(arguments,0);

Method 2: One point worse than method 1 because it creates an array before proceeding


var arg = [].slice.call(arguments,0);

Method 3: Convert to Array by Loop


function toArray(arguments){
  var a = [];
  for(var i=0;i<arguments.length;i++){
    a.unshift(arguments.[i]);
  }
  return a;
}

caller

When a function is called by another function, the called function automatically generates an caller attribute that points to the function object calling it. If the function is not called, caller is null.


function testCaller() {
  var caller = testCaller.caller;
  alert(caller);
}
function aCaller() {
  testCaller();
}
aCaller();

What pops up is the content of the function aCaller.

arguments.callee
arguments.callee points to the running function itself and returns the Function object being executed, which is the body of the specified Function object.
Note: arguments.length is the actual parameter length, arguments.callee.length is the formal parameter length, which is usually used to determine whether the length of the formal parameter is the same or not.
The actual parameters of the function are obtained from arguments, and the formal parameters of the function are obtained from arguments.callee.
Closures are also widely used.


var i = 0;

  function b(num) {

    if (num < 10) {

      num++;

      i++;

      // If there are parameters, callee Also take the parameters with you ;

      arguments.callee(num);

    } else {

      // output 2 second 

      alert(" Called "+i+" second callee!");

    }

  }

  b(8);

 Arguments.callee In the case of closures, it provides 1 The function of seed recursive calls. 

// use arguments.callee Calculation 10 factorial , for example : 1 × 2 × 3 × 4 × 5 × 6 × 7....

  function c(x) {

    return x > 1 ? x * arguments.callee(x - 1) : 1

  } (10);

  // output 6

  alert(c(3));

  // output 3628800

  alert(c(10));

Example: callee sums 1-n


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

It allows an anonymous function to call itself

Example:


function list(type){
  var result = "<"+type+"l><li>";
  var args = Array.prototype.slice.call(arguments,1);
  result += args.join("</li><li>");
  result += "</li></"+type+"l>";
  return result;
}
var listHtml = list("o","one","two");
console.log(listHtml);

Example 2: Interview Question: What is the result of console.log below [1,2,3,4]?


alert(arguments instanceof Array);
alert(arguments instanceof Object);
0

At the time of pre-interpretation, function fn(){}(1);It is separated into two functions, the first being function fn() {} and the second being anonymous: (1).If the second parameter is not used, an error will be made, but the function above is correct if it is contained in one ().


alert(arguments instanceof Array);
alert(arguments instanceof Object);
1


Related articles: