js--function parameters (you may not know the arguments passed)

Source: Internet
Author: User

Objective:

The function is divided into a parameter with a return value, a parameter has no return value, no parameter has no return value, and no parameter has a return value; So what about the function you want to use without parameters? What if you want to encapsulate a code that implements a variety of functions, but the parameters are larger than the arguments or the arguments are larger than the formal parameters? In this paper, some related operations about parameters, such as value passing, parameter processing principle, etc., are expounded in the process of function call.

1. The real parameter is greater than the shape parameter:

function Say (name,message) {    console.log (' Hello ' + name + message);  }  Say (' world! ', ' byebye! ', ' world ');

Console print out: helloworld! byebye!

2. The real parameter is less than the shape parameter:

function Say (name,message) {    console.log (' Hello ' + name + message);  }  Say (' world! ');

Console print out: helloworld!undefined

When the variable is defined in JS, if a variable is not assigned an initial value then the variable is of type Undefiend

Careful to find a rule, that is, in the function call, regardless of the number of arguments greater than or less than the parameters of the function is called to execute; in JS, the function does not mind passing in the number of arguments, also do not care about the data type of the passed in parameters. When a function call can be given to an argument or to multiple arguments, this is because the parameters in JS are internally represented by an array . This array is always received by the function, regardless of which parameters are included in the array, and if the array contains no arguments, it is OK to include multiple parameters, which can be accessed through the arguments (parameter) object in the body of the function to get each parameter passed to the parameter. Arguments objects and array objects are similar, you can get each element passed in by subscript (the first element is arguments[0]), or you can use the Length property to determine how many parameters are passed in.

3. Use arguments to implement the "call" of the function parameter:

For the above code, you can write this:

  function say (name, message) {      console.log (' Hello ' + arguments[0] + arguments[1]);      Console.log (arguments.length);    }
Say (' world! ', ' byebye! ’);

Or

function say () {    console.log (' Hello ' + arguments[0] + arguments[1]);    Console.log (arguments.length);  }  Say (' world! ', ' byebye! ’);

The console prints the same effect; that is: helloworld! byebye!

That is, here you can understand that when a function call occurs, the argument is stored in an "array" called arguments, while the corresponding underlying value in the arguments is always maintained with the parameters of the called function when the function call occurs;

For this, the following methods can be used to verify:

function say (name, message) {      Console.log (arguments[1] = = message);      Arguments[1] = ' world! ';      Console.log (arguments[1] = = message);    }    Say (' world! ', ' byebye! ’);

Console printing: true;true;

However, it is not said that reading these two values will access the same memory space, their memory space is independent, but their values will be synchronized, the synchronization is unidirectional, that is, the parameter changes will not change the corresponding value in Argumens.

function say (name, message) {      console.log (arguments.length);    }    Say (' world! ‘);

Control printing: 1;

The length of the arguments object is determined by the number of arguments passed in, not by the number of parameters that define the function.

js--function parameters (you may not know the arguments passed)

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.