Learn JavaScript from the beginning (vii)--functions

Source: Internet
Author: User

Original: Learn JavaScript from the beginning (vii)--function

First, return

The function stops and exits immediately after executing the return.

return value, with return;

Here are two examples:

1 function sum (NUM1, num2) {2             num1= num1 + num2; 3             return NUM1; 4         }56         var result = SUM (5, ten); 7         alert (result);//15
function sum (NUM1, num2) {            Num1= num1 + num2;             return ;        }         var result = SUM (5, ten);        alert (result);//undefined;

Cases where return directly without any parameters are typically used in cases where the function execution needs to be stopped prematurely without returning a value.

As follows:

function counter () {            for (var count = 1;; count++) {                + "A");                  if (Count = = 5)                    {return;                }                 + "B");            }             + "C");          }        Counter ();

Second, the parameters

The characteristics of ECMAScript function parameters;

    • Don't mind passing in the number of parameters
    • Don't mind passing in the data type of the parameter

The two features above are because the ECMAScript parameter is internally represented by an array. The function always receives this array, regardless of which parameters are included in the array, or whether there are parameters.

In fact, the parameter array can be accessed through the arguments object in the body of the function to obtain each parameter passed to the function.

For example:

function sayhi (name, message) {            alert ("Hello" + name + "," + message);            }        Sayhi ("Yang Xiao", "How is you today?");

Can also be written as:

function Sayhi () {            alert ("Hello" + arguments[0]+ "," + arguments[1]);            }        Sayhi ("Nicholas", "How is you today?");

So, in JavaScript, naming parameters is just a convenience, not a requirement.

using Arguments.length, you can tell how many parameters are passed to the function , for example:

function Howmanyargs () {            alert (arguments.length);        }                Howmanyargs ("string");    // 2        Howmanyargs ();                // 0        Howmanyargs (n);              // 1

With this, you can let the function accept any parameter and implement the corresponding function separately:

1     functionDoadd () {2             if(Arguments.length = = 1) {3Alert (Arguments[0] + 10);4}Else if(Arguments.length = = 2) {5Alert (Arguments[0] + arguments[1]);6             }7         }8         9Doadd (10);// -TenDoadd (30, 20);// -

Arguments can also be used with named parameters:

1   functionDoadd (NUM1, num2) {2             if(Arguments.length = = 1) {3Alert (NUM1 + 10);4}Else if(Arguments.length = = 2) {5Alert (Arguments[0] +num2);6             }7   }       8Doadd (10);// -9Doadd (30, 20);// -

The value of arguments is always synchronized with the value of the corresponding named parameter:

 1  function   Doadd (NUM1, num2) { 2  alert (argument S[0] + num2);  3   4
      5  Doadd (10, 20); //3  0     6  Doadd (30, 20); // 50  
1  function Doadd (NUM1, num2) {2                 arguments[1] = ten; 3             Alert (Arguments[0] + num2); 4         }56         Doadd (ten);        //  - 7         Doadd (+);    //  +

Each time the Doadd function is executed, the second parameter is rewritten (the second parameter is re-assigned to 10 in the above example). This is because the values in the arguments object are automatically reflected in the corresponding named arguments, so modify the

ARGUMENTS[1], which is equivalent to modifying the num2, but their memory space is independent, the value is synchronized.
Named parameters that do not pass the value will be given undefined, for example:
1         function Doadd (NUM1, num2) {2             alert (num2);   undefined3        }45         Doadd (10);        
Arguments also has a property: Callee, which functions to return the function that is being executed
For example:
1         function Argumentstest (A, b) {2            alert (Arguments.callee);//returns the function itself 3        } 4         Argumentstest (1,2,3,4);

It is recommended to use Arguments.callee instead of the function name itself when using recursive function calls.

For example:

function Count (a) {                 if(a==1) {                         return 1;                     }                  return A + arguments.callee (-a);             }          var mm = count (2);         alert (mm);
Arguments.length Returns the number of arguments passed to a function, also called an argument, and what does Arguments.callee.length return?
Look at an example:
1 function Calleelengthdemo (arg1, arg2) {2                 alert ("Arguments.length:" +arguments.length);   33                 alert ("Arguments.callee.length:" +arguments.callee.length);   245        }6         Calleelengthdemo (n/a);

It can be seen from this that Arguments.callee.length returns a function-defined parameter, the formal parameter.

Three, no overloading

The ECMAScript function cannot be overloaded, cannot define the same function, and then executes different functions based on different parameters with the compiler.

For example:
1  functionAddsomenumber (NUM1) {2             returnNUM1 + 100;3         }4         5         functionAddsomenumber (num2) {6             returnNUM2 + 200;7         }8         9         varresult = Addsomenumber (100);// -Tenalert (result);
1 functionAddsomenumber (NUM1) {2num1+=100;3             returnNUM1 + 100;4         }5         6         functionAddsomenumber (num2) {7             returnNUM2 + 200;8         }9         Ten         varresult = Addsomenumber (100);// - Onealert (result);
as long as the function name is consistent (Addsomenumber in the example above), ECMAscript will think of the same thing, then the definition will overwrite the first definition. 
for ECMAScript function, I have no parameters, you give me what parameters I accept what parameters. 

Learn JavaScript from the beginning (vii)--functions

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.