arguments object to learn javascript from me

  • 2020-10-31 21:37:43
  • OfStack

1. What is arguments

arguments is one of the built-in objects in JavaScript. It's weird and often overlooked, but it's actually very important. All the major js function libraries make use of the arguments object. So the agruments object must be familiar to the javascript programmer. In the body of the javascript function, the identifier arguments has a special meaning. It is a special property of the calling object that references the Arguments object. The Arugments object is like an array, notice that it's just like it's not.

In the body of the javascript function, arguments looks like an array (not a real array, but an Arguments object, again)1, and has the length attribute, which represents the number of arguments passed to the function.

In javascript, the Arguments object is the actual parameter of the function, and the length of the arguments object is determined by the number of arguments, not the number of formal arguments. A formal parameter is a variable that reopens the memory space for storage within the function, but does not overlap with the arguments object memory space.

js does not actively determine how many arguments you pass to a function. If you pass too many, the extra is not used. If you pass too few, the unpassed value is undefined. So we can use the length attribute of arguments to check that the function is being called with the right number of actual arguments, because javascript won't do that for you


function f(x,y,z)
{
 // First check that the number of parameters passed is correct 
 if(arguments.length != 3)
 {
  throw new Error("function f called with " + arguments.length + "arguments");
 }
 // Now let's run the actual function 
}

2. arguments creates a variable parameter list function

arguments also provides us with the possibility of passing any number of actual parameters to a function:

For example, I want to use an display() function to calculate the payroll for each company, yes, yes, you can pass as many parameters as you want, but only if you pass the Numbers, because I'm too lazy to judge inside the function.


 function display(){
  var sum=0; // The total amount of 
  for(var i=0;i<arguments.length;i++){
   sum+=arguments[i];
  }
  document.write(sum+'<br>');
 }

 //A The company 
 display(10000,2000,5000);
 //B The company 
 display(1000,2000,5000,8000,10000);

How's that? Isn't that clever?

Note 1 The formal parameters of arguments and the real transmission are 1:

In the case of both arguments and values, the two values change synchronously with one of them, that is, they change all the values of the two


function f(a, b, c){
 alert(arguments.length); // result: "2"
 a = 100;
 alert(arguments[0]);  // result: "100"
 arguments[0] = "qqyumidi";
 alert(a);     // result: "qqyumidi"
 alert(c);     // result: "undefined"
 c = 2012;
 alert(arguments[2]);  // result: "undefined"
}

f(1, 2);

3. Never modify the arguments object

The connection between the parameters declared in the function and arguments is fragile, and each declared parameter is really just one reference to the corresponding location in the arguments object.

It is worth noting that in strict mode of ES5, the arguments declared by the function do not refer to arguments:


function strict(x) {
 "use strict";
 arguments[0] = "modified";
 return x === arguments[0];
}
function nonstrict(x) {
 arguments[0] = "modified";
 return x === arguments[0];
}
strict("unmodified"); // false
nonstrict("unmodified"); // true

Because in strict and non-ES70en modes, the arguments declared by functions are not related to arguments, it is safest not to modify the arguments object in order to avoid problems.

If you do need to modify the arguments object, you can first assign a copy of the arguments object:

var args = [].slice.call(arguments);
When the slice method accepts no arguments, the copy is performed and the resulting args is also a real array object. At the same time, there is no connection between args and the parameters declared by the function, and it is safe to operate on it.

4. 1 variable to hold references to arguments

Suppose you need an API to traverse through several elements, as follows:


var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6); 
it.next(); // 1 
it.next(); // 4 
it.next(); // 1 

The corresponding implementation can be:


function values() { 
 var i = 0, n = arguments.length; 
 return { 
  hasNext: function() { 
   return i < n; 
  }, 
  next: function() { 
   if (i >= n) { 
    throw new Error("end of iteration"); 
   } 
   return arguments[i++]; // wrong arguments 
  } 
 }; 
} 

But the reality of the implementation is:


var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6); 
it.next(); // undefined 
it.next(); // undefined 
it.next(); // undefined 

The reason is that the assignment to the arguments object is done implicitly. Within the next method, arguments is used, however the arguments and values at the beginning of the arguments and values methods are not one object. The arguments object here is of the function next().

The solution is simply to reference the arguments that needs to be accessed using another variable. It is then accessed in its nested functions through the nature of the closure, as follows:


function values() { 
 var i = 0, n = arguments.length, a = arguments; 
 return { 
  hasNext: function() { 
   return i < n; 
  }, 
  next: function() { 
   if (i >= n) { 
    throw new Error("end of iteration"); 
   } 
   return a[i++]; 
  } 
 }; 
} 
var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6); 
it.next(); // 1 
it.next(); // 4 
it.next(); // 1 

5. callee attribute of arguments object:

The callee attribute of arguments is used to refer to the currently executing function, which is very beneficial to the unnamed function call itself.

First, the recursive function is realized by using the named function expression:


// Functional direct quantity   Specify function name   Recursive function 
var result = function fact(x){
 if(x<=1) 
  return 1; 
 else 
  return x*fact(x-1);
};

In that case, I mentioned that you could call a function a direct quantity by a function name. This makes it easy to call yourself recursively.

Now callee with arguments is also easy to implement


// I'm going to use a direct function, and I'm going to use arguments.callee Properties implement recursive functions 
var result = function(x){
 if(x<=1) return 1; 
 return x*arguments.callee(x-1);
};

One final point, since arguments is so good, let's not name the variable arguments. In fact, arguments is one of the reserved words of javascript. Well.

One last point:

The difference between caller

Returns 1 reference to the function that called the current function.

- functionName.caller The -ES144en object is the name of the function being executed.

For functions, the caller attribute is defined only at function execution time. If the function is called by the top level, then caller contains null. If you use the caller attribute in the context of a string, the result is the same as functionName.toString 1, that is, the decomcompiled text of the function is displayed.

Code:


 function display(){
  var sum=0; // The total amount of 
  for(var i=0;i<arguments.length;i++){
   sum+=arguments[i];
  }
  document.write(sum+'<br>');
 }

 //A The company 
 display(10000,2000,5000);
 //B The company 
 display(1000,2000,5000,8000,10000);

0

The above is the relevant introduction of arguments object for javascript, I hope it will be helpful for your study.


Related articles: