Fundamentals of parameter processing

Source: Internet
Author: User

One, you can pass any number of parameters

When invoking a function, you can pass as many arguments as you want, regardless of how many formal arguments the function declares. The value of the missing parameter is undefined, and the extra parameters are ignored directly.

Second, all parameters passed are stored in arguments.

All of the passed parameters are stored in a very special variable, much like an Array (which continues to see why)--arguments.

Let's look at how this variable is used by the following function:

function g () {    console.log (' Length: ' +arguments.length);    Console.log (' Elements: ' +FromArray (arguments));//FromArray function, which converts the arguments into an array so that it can be stored in the data}

Call G ():

>0Elements:> G (' A '1elements:a> G (' A ', ' B '2elements:a,b 

No matter how many parameters are explicitly declared, arguments is always there, and it contains all the actual arguments.

If the caller does not provide a parameter, it is undefined passed to function . You do not pass the argument with the undefined result of the incoming obtained is the same

  而传入false, 0 and empty strings are parsed as missing parameters

Tips:

We can use it || to specify the default value of the parameter:

function add (x, y) {
x = x | | 0;
y = y | | 0;
return x + y;
}

Fundamentals of parameter processing

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.