Parameters and return statements for the javascript-function

Source: Internet
Author: User

xTable of Contents [1] parameter [2]arguments object [3] function overloading [4]return statement

-------------------------------------

I. Parameters (Max . 25)

You can dynamically change the type or value of the corresponding variable in the function body so that the same function body gets different results.


formal parameters : When defining a function, the variables defined in the function brackets are called formal parameters.

arguments : When a function is called, the variable or value passed in parentheses is called an argument.

1. Types of Parameters

Can be any type of data

  2. Number of parameters (up to 25)

A. The number of arguments and formal parameters is equal and corresponds to each other.

B. Number of formal parameters more than actual arguments
************************************************
will not error, but the extra parameters of his value, will be automatically assigned to the value of undefined
************************************************

The number of formal parameters is more than the actual parameter

  function Fun1 (A, b) {    alert (a);    alert (b);  }  FUN1 (1)

Test results:


C. The number of arguments is more than formal parameters
************************************************
No error, but to get the value of the extra arguments, use the arguments object
************************************************

Arguments more than formal parameters

function Fun (A, b     ) {for ( var i=0; i<arguments.length; i++) {         alert (Arguments[i])     }  }  Fun (1,2,3)//Note inside the parameter "3"

Test results

-------------------------------------

Second, arguments object

Each time a function is created, the function implicitly creates a arguments object that contains information about the actual incoming parameters.

1.length Detecting the number of actual incoming parameters

function aa (A, b) {if(arguments.length==1) {alert ("there is only one parameter:"+a); }   if(arguments.length==2) {alert ("the two parameters are:"+a+","+b); }  if(arguments.length>2) {alert ("too many parameters."); } }


2.callee calls to itself
Accesses the specific value of the passed-in parameter. ([subscript]) //Refer to the above argument for greater than formal parameter code and test results

-------------------------------------

Third, function overloading

The same function can correspond to the implementation of multiple functions because of the different type or number of parameters, each of which corresponds to a function body

-------------------------------------

Four, return statement

one, stop and jump out of the current function
1. All content in the function body after the Ruturn statement is not output.
2. There can be multiple return statements in the function body, but only one is executed. (Judgment statement)

returns a value return [return value] to the function;
1. The return value can be any data type
2. Only one return value can be returned.
3. If the function does not return a value, the value of the function is automatically assigned to undefined

Parameters and return statements for the javascript-function

Related Article

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.