JS Elevation 3. Basic Concept (6) function

Source: Internet
Author: User

Functions in 1.ECMAScript are declared using the function keyword.

eg

1 function sum (num1,num2) {2     alert (num1+num2); 3 }4  sum (3,7);

Attention:

In a function with a return statement, the function stops and exits immediately after executing the return statement. Therefore, any code that is behind the return statement will never be executed.

eg

1 function sum (num1,num2) {2     return num1+num2; 3     Alert ("hello!"); // Will never be executed.  4  }5  alert (sum (3,7));

  When the above code executes, a warning box with the content "10" will appear, but the content will not appear as "hello! Warning box, because the function has finished executing the return statement and will stop the end and will not execute the following alert ("Hello! ") statement.

2. Some restrictions on functions in strict mode

(1) The function cannot be named Evel or arguments;

(2) The parameter can not be named Evel or arguments;

(3) You cannot have two named parameters with the same name.

3. Understanding Parameters

(1) ECMAScript parameters in the internal is represented by an array, the function received is always the array, the ECMAScript function does not mind passing in the number of arguments and parameters of the data type, from this point can be seen, ECMAScript function cannot implement overloading in the traditional sense.

The definition of overloaded functions: In the same declaration field, the function name is the same, and the parameter table is different, that is, a special function that uniquely identifies and distinguishes the function by the parameter table of the function.

(2) The function body can access the parameter array through the arguments object , thus obtaining each parameter passed to the function.

  

1  function sum () {2     return arguments[0]+arguments[1]; 3 }4  alert (sum (3,6));

The above example shows:

  The parameters named by the ECMAScript function are only convenient, but are not required.

(3) The length property of the arguments object tells you how many arguments are passed to the function.

eg

1 function HOWMANYARGW () {2    alert (arguments.length); 3 }4  //25  HOWMANYARGW ();     // 0 6  HOWMANYARGW (n);   // 1

There are three prompt boxes, followed by:

(4) Developers can take advantage of this to allow the function to receive arbitrary parameters and to implement the appropriate functions separately .

eg

 1  function   Doadd () { 2   If  (Arguments.length==1 3  alert (Arguments[0]+10 4 }else  if< /span> (Arguments.length==2 5  alert (AR Guments[0]+arguments[1 6  }  7  }  8  doadd (20 9  doadd (21,10); 

(5) Arguments objects can be used with named parameters:

eg

1  functionDoadd (num1,num2) {2         if(arguments.length==1){3AlertNUM1+10);4}Else if(arguments.length==2){5Alertarguments[0]+ num2);6         }7     }8Doadd (20);9Doadd (21,10);

(6) The value of arguments is always synchronized with the value of the corresponding named parameter.

eg

1  function Add (num1,num2) {2         arguments[1]=10;  // each execution of the Add function overrides the second parameter, changing the value of the second parameter to ten. 3         Alert (arguments[0]+num2); 4     }5     Add (+);   The original value is 1+2=3, because the value of the second parameter is changed 1+10=11.

Attention:

(1) This is not to say that two values will access the same memory space, their memory space is independent, but their values will be synchronized.

(2) A named parameter that does not pass a value is automatically assigned the undefined value.

eg

1 function Add (num1,num2) {2         arguments[1]=10; 3         Alert (arguments[0]+num2); 4     }5     alert (typeof(num2)); 6     Add (1);

The prompt box appears in turn:

(3) All parameter passing in ECMAScript is a value, and it is not possible to pass arguments by reference.

  

JS Elevation 3. Basic Concept (6) function

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.