There are three ways to define a function:
- keyword function to define :
- function declaration statement Functions f () {}
- 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:
- 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.
- A pair of parentheses ().
- A pair of curly braces {}.
So the above two forms are actually like this:
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