Definition of function

Source: Internet
Author: User

There are three ways to define a function:

    • keyword function to define :
    1. function declaration statement Functions f () {}
    2. function definition expression: Common form: var f = function () {}; There are no fewer semicolons here.
    • Use constructor function: var f = new function (), pass in the parameter, the last parameter defaults to the function body, the preceding argument is the function parameter. (This method is not recommended because this method causes parsing of two code, 1: General parsing of the Emcascript code, and 2: parsing the strings passed into the constructor, which can affect performance.) )

Eg:var sum = new Function ("Num1", "num2", "return NUM1 + num2");

The function is defined in the first two ways:

    • function sum (NUM1, num2) {return num1 + num2;}
    • var sum = function (NUM1, num2) {return num1 + num2;} ;

Using function declaration statements and function definition expressions contains three parts:

    1. The function name identifier, which is funcname, but not the same as the requirement, declares the statement to be necessary, and the function definition expression is optional , usually omitted.
    2. A pair of parentheses ().
    3. A pair of curly braces {}.

So the above two forms are actually like this:

  • Declaration statement: Function FuncName (x) {}, in the case of a declaration statement, a funcName variable is actually declared and an object is assigned to it.
  • Function-Definition Expressions: FunctionsFuncName(x) {}, the function definition expression does not declare a variable, where the funcname is optional (optional), (If there is a funcname, the local scope of the function contains a name that is bound to the function object, that is, funcname,funcname refers to the function object, and the value exists inside the function body, that is, the function name becomes a local variable of the function.), the function definition expression is usually used only as part of a large expression: assignment, or invocation, so there is: var s = functionFuncName(x)  {}; usually omit the function name, that is, the common var s = function (x) {}; Typically, the name of a function expression is useful when recursion, such as
    var function Fact (x) {    if(x <=1) {        return 1;    } Else {        return x * FACT (x-1);   Recursive use of the function    }}

Both of these definitions will create a new function object. The function name is actually a pointer to the function object and is not bound to a function.

The meaning is that the function body is there, does not move, you can use the function name to refer to the function to operate, the two are relatively independent, but have some relationship. Look at the following example:

1 functionsum (num1,num2) {2     returnNUM1 +num2;3 }4Alert (sum (10,10));// -5 6 varAnothersum =sum;7Alert (Anothersum (10,10));// -8sum =NULL;9Alert (Anothersum (10,10));// -

The sum here is the function name, which can be referenced by sum to the function, the value of sum 6th will be assigned to Anothersum, so that anothersum can also access the function body, the 8th row of variable sum is set to NULL, that is, it and the function of the relationship between the body cut off, But the function body is still independent, not affected, so anothersum can still refer to it, so the variable name is just a pointer.

After understanding the pointer, then take a look at the function overload, there is no concept of function overloading in emacascript, what is overloading it?

1 functionaddsomenumber (num) {2     returnNum + 100;3 }4 functionaddsomenumber (num) {5     returnNum + 300;6 }7 varresult = Addsomenumber (100);// -8 

In a different way:9 varAddsomenumber =function(num) {Ten returnNum + 100; One }; AAddsomenumber =function(num) { - returnNum + 300; - }; the - varresult = Addsomenumber (100);// -

So no overloading is actually, overriding a function reference, when the second function body is created, the function name points to the newly created function body, overwriting the previously referenced function body,

A function declaration differs from a function-definition expression:

The main difference is that the function declaration is in advance, a bit like defining a variable with VAR, the variable declaration is ahead of time, but the initialization is still in the original position, but the function declaration causes both the function name and the body to be added to the execution environment, so you can use the function before declaring the function. The function definition expression cannot be advanced and must be executed before it is valid.

Alert (sum (10,10));          // The fame is ahead of you function sum (num1,num2) {    return num1 + num2;} Alert (sum (10,10));           // There 's going to be an error here . var function (NUM1, num2) {    return num1 + num2;};

The difference is when you can access the function. The others are equivalent.

12:45:22, 2016-01-10

Definition of 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.