JavaScript's Magical parameters

Source: Internet
Author: User

The parameters of the JS function differ greatly from other languages. It doesn't matter how many parameters you pass over, or what type of arguments are passed in. Even if you define a function that accepts only two parameters, you can pass one, three, or even no arguments when you call this function. This is because the parameters in JavaScript are internally represented by an array . This array is always received by the function, not the parameters contained in the array (if any). In the body of the function we can access this array through the arguments object to get each parameter passed to the function. You can look at the following example:

function Sayhi (name,message) {    alert ("Hello" + name + "," + message);}
function Sayhi () {    alert ("Hello" + arguments[0] + "," + arguments[1]);}

The first paragraph of the

Code is passed by a named parameter, and the second code overrides the first. This rewritten function does not contain named arguments. Although the name and message identifiers are not used, the function is still functional. This fact illustrates an important feature of the JavaScript function: . In addition, in terms of named parameters, other languages may need to create a function signature,  in advance and future calls must be consistent with that signature. But in JavaScript,,  does not have these rules, and the parser does not validate named parameters. The accesses the length property of a arguments object to know how many arguments are passed to the function . Developers can take advantage of this to enable a function to receive any parameter and implement the appropriate functionality separately . Take a look at the following example:

function Doadd () {if (arguments.length = = 1) {    alert (arguments[0] +tenelse if (Arguments.length = = 2) {    alert (arguments[0] + arguments[1]);} Doadd (ten);    // Doadd (30,20);    //  -

We all know that there is no overload in JavaScript , and using the characteristics of the above example to achieve a less perfect overload , but also to compensate for the JS of this flaw.

The arguments object can be used with named parameters , and the value of arguments is always synchronized with the value of the corresponding named parameter . As shown in the following example:

function Doadd (NUM1, num2) {    arguments[1] = ten;    Alert (arguments[0] + num2);}

Each execution of the Doadd () function overrides the second parameter, modifying the value of the second parameter to 10. Because the values in the arguments object are automatically reflected in the corresponding named arguments , modifying the arguments[1] also modifies the num2, resulting in their values becoming 10. However, this is not to say that reading these two values accesses the same memory space; their memory space is independent, but their values are synchronized . Also keep in mind that if only one parameter is passed in, the value set for arguments[1] is not reflected in the named parameter . This is because the length of the arguments object is determined by the number of arguments passed in, not by the number of named arguments when the function is defined. And a named parameter that does not pass a value num2 is automatically assigned the undefined value . This is the same as defining a variable without initializing it.

JavaScript's Magical parameters

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.