Understanding javascriptarguments objects and function parameters

Source: Internet
Author: User
The javascriptarguments object is mainly learned in the function section of javascript. Viajavascript arguments objects are mainly learned in the javascript function section.

Iii. argument object

In fact, the function body can use an argument object to receive passed parameters.

Function box () {return arguments [0] + '|' + arguments [1]; // obtain the value of each parameter} alert (box ('caibaojian ', 24 ));

The length attribute of the arguments object to obtain the number of parameters.

function box(){return arguments.length; }alert(box(1,2,3,4,5)); 5

Today, we will mainly talk about the usage process. We will use an example to illustrate the differences between the parameters used by the arguments object to access the function and the parameter defined by the row parameter.

// Code from http://caibaojian.com/js-arguments.htmlfunction func (a, B) {B = 30; arguments [0] = arguments [1] return a;} function (1, 2); 30 function (1 ); undefined // because the arguments parameter in the function is changed, a = arguments [1], arguments [1] is not defined, so a finally outputs undefined

Link: http://caibaojian.com/js-arguments.html

Number of via detection parameters from: http://caibaojian.com/js-arguments.html

You can also use the arguments object to check the number of parameters of the function and reference the arguments. length attribute.

The following code outputs the number of parameters used for each function call:

function howManyArgs() {  alert(arguments.length);}howManyArgs("string", 45);howManyArgs();howManyArgs(12);

The above code will show "2", "0", and "1" in sequence ".

Note: unlike other programming languages, ECMAScript does not verify that the number of parameters passed to a function is equal to the number of parameters defined by the function. All functions defined by the developer can accept any number of parameters (according to Netscape documentation, a maximum of 255 parameters can be accepted) without causing any errors. Any missing parameters will be passed to the function as undefined, and additional functions will be ignored.

Simulate function Overloading

You can use the arguments object to determine the number of parameters passed to the function to simulate function overloading:

Function doAdd () {if (arguments. length = 1) {alert (arguments [0] + 5);} else if (arguments. length = 2) {alert (arguments [0] + arguments [1]) ;}} doAdd (10); // output "15" doAdd (40, 20 ); // output "60"

If there is only one parameter in via, The doAdd () function adds 5 to the parameter. If two parameters exist, the two parameters are added and their sum is returned. Therefore, doAdd (10) Outputs "15", while doAdd (40, 20) Outputs "60 ". From: http://caibaojian.com/js-arguments.html

It is not as good as heavy load, but it is enough to avoid the restriction of ECMAScript.

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.