Introduction to the use of javaScript arguments objects

  • 2020-03-26 21:28:50
  • OfStack

ECMAScript functions don't care how many arguments are passed in, and they don't go wrong because the arguments are inconsistent. In fact, the body of the function can receive arguments passed in through the arguments object.
function box() { 
return arguments[0]+' | '+arguments[1]; //Get the value of each argument

alert(box(1,2,3,4,5,6)); //Passing parameters

arguments  The object's  length  Property to get the number of parameters.  
function box() { 
return arguments.length; //Get 6


We can use the length property to intelligently determine how many parameters there are, and then apply the parameters reasonably.
For example, to perform an addition operation, add up all the Numbers passed in, and the number of Numbers is uncertain.
function box() { 
var sum = 0; 
if (arguments.length == 0) return sum; //If no arguments, exit
for(var i = 0;i < arguments.length; i++) { //If so, add up
sum = sum + arguments[i]; 
return sum; //Return the cumulative result


ECMAScript  Function, which does not have the function overloading capabilities of other high-level languages.  
function box(num) { 
return num + 100; 
function box (num) { //It's going to execute this function
return num + 200; 
alert(box(50)); //Returns the result

Related articles: