JavaScript Arguments Object Usage Introduction _ Basics

Source: Internet
Author: User
The ECMAScript function does not care how many arguments are passed in, nor does it error because the parameters are not uniform. In fact, a function body can receive incoming arguments by arguments the object.
Copy Code code as follows:

function box () {
Return arguments[0]+ ' | ' +arguments[1]; Get the value of each parameter
}

Alert (Box (1,2,3,4,5,6)); Passing parameters

The length property of the arguments object can get the number of parameters.
function box () {
return arguments.length; Get 6
}

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


We can use the attribute of length to intelligently judge how many parameters there are, and then apply the parameters reasonably.
For example, to implement an addition, add all the numbers that come in, and the number of numbers is uncertain.
Copy Code code as follows:

function box () {
var sum = 0;
if (arguments.length = 0) return sum;///If there are no arguments, exit
for (var i = 0;i < Arguments.length; i++) {//if any, add
sum = sum + arguments[i];
}
return sum;//returns the cumulative result
}

Alert (Box (5,9,12)); The functions in the

ECMAScript do not have function overloading functions like other high-level languages.
function box (num) {
return num + 100;
}
function box (num) {//will execute this function
return num + 200;
}
Alert (Box (50));//return result

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.