Introduction to javaScriptarguments object use _ basic knowledge-js tutorial

Source: Internet
Author: User
The function body can receive passed parameters through the arguments object. The following is a good example. You can feel that the ECMAScript function does not mind how many parameters are passed in, it will not be wrong because of inconsistent parameters. In fact, the function body can use the arguments object to receive passed parameters.

The Code is as follows:


Function box (){
Return arguments [0] + '|' + arguments [1]; // obtain the value of each parameter.
}

Alert (box (1, 2, 3, 4, 5, 6); // pass the Parameter

The length attribute of the arguments object to obtain the number of parameters.
Function box (){
Return arguments. length; // get 6
}

Alert (box (1, 2, 3, 4, 5, 6 ));



We can use the length attribute to intelligently determine the number of parameters, and then apply the parameters reasonably.
For example, to implement an addition operation, add all the numbers that are passed in, and the number of numbers is uncertain.

The Code is as follows:


Function box (){
Var sum = 0;
If (arguments. length = 0) return sum; // if no parameter exists, exit
For (var I = 0; I <arguments. length; I ++) {// If yes, accumulate
Sum = sum + arguments [I];
}
Return sum; // return the accumulate result.
}

Alert (box (5, 9, 12 ));

Functions in ECMAScript Do not overload functions like other advanced languages.
Function box (num ){
Return num + 100;
}
Function box (num) {// this function will be executed
Return num + 200;
}
Alert (box (50); // return results

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.