{JS} variable length parameter list: Arguments object

Source: Internet
Author: User

{JS} variable length parameter list: Arguments object
In a function, the identifier arguments has a special meaning. it is a special attribute that references the Arguments object. the Arguments object is an array-like object. You can obtain the parameter values passed to the function by subscript. the Arguments object also defines the callee attribute.

The Arguments object can be used in multiple aspects.
1. Check whether the real parameters are correct during function calling.
Function f (a, B ){
If (arguments. length! = 2 ){
Throw new Error ("the function expects two parameters ");
}
// Todo:
}

2. Compile variable parameter functions
Function max (/*...*/){
Var m = Number. NEGATIVE_INFINITY;
For (var I = 0; I <arguments. length; I ++ ){
If (arguments [I]> m ){
M = arguments [I];
}
}
Return m;
}

Var largest = max (3, 5,100, 88, 99, 11,100 );

3. the Arguments object has an extraordinary feature. when a function has a named parameter, the array element of the Arguments object is a synonym for storing the local variables of the function parameter. arguments [] arrays and named parameters are two different methods that reference the same variable. changing the value of a parameter using the parameter name also changes the value obtained through the arguments [] array. changing the parameter value through the arguments [] array also changes the parameter value obtained using the parameter name. for example:
Function f (){
Console. log ();
Arguments [0] = null;
Console. log ();
}

F (1 );

4. finally, remember that arguments is just a common JS tag, not a reserved word. if a parameter or local variable in the function uses this name, it hides the reference to the Arguments object. for example:
Function e (){
Var arguments = 1; // hide a reference to the Arguments object
Console. log (arguments );
}

Function f (){
Console. log (arguments); // still points to the reference of the Arguments object
Var arguments = 1;
}

Function g (arguments) {// hide a reference to the Arguments object
Console. log (arguments );
}

E (1, 2 );
F (1, 2 );
G (1, 2 );

Therefore, it is best to use arguments as a reserved word and avoid using it as a variable name.


By bill200711022

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.