A, definitions,

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.

Arguments can be used to store arguments in order. They have the same access properties and methods as arrays and have the array length property. Arguments are special objects. We know we say arguments when we hear class arrays.

Two, performance content

Arguments is an array-like object that stores the arguments actually passed to a function, so that a function is called beyond the list of arguments defined by the function declaration. So let’s look at some code

function fn (name, age, sex) { console.log(name, age, sex); //Tom 18 undefined // Console. log(fn.length); //3 } fn('Tom', 18);Copy the code

function fn (name, age, sex) {
    console.log(arguments); 
}
Copy the code

As shown in the figure, when fn is null and arguments.length is null, verify that it is used to store the arguments that are actually passed to the function, i.e. the fn called (‘Tom’,18,’boy’).

Three, access,

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

Three, the original interview questions

The following is an interview question about arguments

var length = 10;
function fn() {
  alert(this.length);
}
var obj = {
  length: 5,    
  method: function (fn) {
    alert(this.length) // 5
    fn();            //10
    arguments[0]();  //1
  }
}
obj.method(fn);
Copy the code

The 0 here is a function that performs the method this.length which means the length has only one parameter fn so the length is 1

Fn arguments.this.length (); fn arguments.this.length (); fn arguments.this.length (); fn arguments.this.length ()

Plus another: juejin.cn/post/685967…

What happens when you manipulate a primitive array in a WEBGL JS loop? React ShoudComponentUpdate React ShoudComponentUpdate Stop rendering vue why don’t you use 11 methods for deep copy shallow copy Vue vue $nextTick Deep parsing Deep Revealing Promise microtask registration and execution Process How does the V8 engine execute a section of JS code? Http3.0 2.0 1.0 compare macro tasks and micro tasks in detail