Home > Web Front-end > JS Tutorial > Example explanation of arguments object in Javascript

Example explanation of arguments object in Javascript

不言
Release: 2019-03-05 14:03:45
forward
2051 people have browsed it

This article brings you an example explanation of the arguments object in Javascript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Today we take a look at the arguments object and properties. The arguments object cannot be created explicitly, and the arguments object is only available at the beginning of the function. The arguments object of a function is not an array, and accessing individual parameters is the same as accessing array elements. The index n is actually one of the parameters of the arguments object’s 0…n property.

1 function add(a,b){
2     console.log(typeof arguments);
3     for(var attr in arguments){
4         console.log(attr+":"+arguments[attr]);
5     }
6     return a+b;
7 }
8 
9 add(10,20)
Copy after login

 

Through the output results, we can see that arguments is actually an object, not an array, and this array has two attributes, the attribute names are 0 and 1, and their values 10 and 20 respectively

In JavaScript, the parameter list is divided into formal parameters and actual parameters. The formal parameters are the parameters specified when defining the function, and the actual parameters are the parameters specified when calling the function. For example, in the above example, you can call the function through

alert(add(10))
Copy after login

. Only one parameter is specified here, that is, the number of actual parameters is 1 and the number of formal parameters is 2. In JavaScript, there is no requirement that the number of actual parameters must be the same as the number of formal parameters. The above code can also be executed. Just output NaN

In practice, we can do this, first determine the number of parameters

 1 function add(a,b){
 2     
 3     //add.length也可以获取形参个数,但实际中用arguments.callee.length
 4     if(arguments.length==arguments.callee.length){
 5         return a+b;
 6     }else{
 7         return "参数错误";
 8     }
 9     
10 }
Copy after login

arguments.length: Get the number of actual parameters

arguments.callee .length: Get the number of formal parameters

arguments.callee: refers to the function itself

arguments are often used in recursive operations

For example, find a number from 1 to n and

 1 function fn(n){
 2     
 3     if(n==1){
 4         return 1;
 5     }else{
 6         return n+arguments.callee(n-1);
 7     }
 8 }
 9 
10 alert(fn(100))
Copy after login

results in 5050

The above is the detailed content of Example explanation of arguments object in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template