Original: JavaScript function functions Type
This article mainly introduces the common function, anonymous function, closure function
1. General Function Introduction 1.1 example
function ShowName (name) { alert (name);}
Overwrite of the same name function in 1.2 js
In JS, the function is not overloaded, define the same function name, different parameter signature function, the following function will overwrite the previous function . When called, only the following function is called.
var n1 = 1;function Add (value1) { return n1 + 1;} Alert (Add (N1));//The following function is called, Output: 3function Add (value1, value2) { return value1 + 2;} Alert (Add (N1));//output: 3
1.3 Arguments Object
arguments is similar to the paramsof C # , manipulating mutable parameters: The number of arguments passed in the function is greater than the number of arguments when defined.
function ShowNames (name) { alert (name);//Zhang San for (var i = 0; i < arguments.length; i++) { alert (arguments[i ]);//Zhang San, John Doe, Harry }}shownames (' Zhang San ', ' John Doe ', ' Harry ');
1.4 Default range values for functions
If the function does not indicate the return value, the default return is ' undefined '
function ShowMsg () {}alert (ShowMsg ());//output: undefined
2. Anonymous function 2.1 Variable anonymous function 2.1.1 Description
You can assign a function to a variable, an event.
2.1.2 Example
Variable anonymous function, left can be variable, event var anonymousnormal = function (P1, p2) { alert (P1+P2);} Anonymousnormal (3,6);//Output 9
2.1.3 Applicable Scenarios
① avoid pollution of function names. If you declare a function with a name, and then assign it to a variable or event, it causes the misuse of the function name.
2.2 No Name anonymous function 2.2.1 Description
That is, at the function declaration, immediately following the argument. JS syntax when parsing this function, the inside code executes immediately.
2.2.2 Example
(function (p1) { alert (p1);}) (1);
2.2.3 Applicable Scenarios
The ① only needs to be executed once. If the browser is loaded, you only need to perform the function once and do not perform it later.
3. Closure Function 3.1 Description
Suppose that function B is declared internally, function B refers to a variable other than function B, and the return value of function A is a reference to function B. Then function B is the closure function.
3.2 Example 3.2.1 Example 1: Global Reference and Local reference
function Funa () { var i = 0; function Funb () {//Closure function Funb i++; Alert (i) } return FUNB;} var Allshowa = Funa (); Global variable reference: Cumulative output 1,2,3,4 such as function Partshowa () { var Showa = Funa ();//local variable reference: Output only 1 showa ();}
Allshowa is a global variable that references the function Funa. Running Allshowa () repeatedly will output a cumulative value such as 1,2,3,4.
Executes the function Partshowa (), because internally only the local variable Showa is declared to refer to the Funa, and the resources that are consumed by the Showa are released because of the scope relationship after execution.
the key to closures is scope : the resources that the global variables occupy are freed only when the page is transformed or the browser is closed. var Allshowa = Funa (), the equivalent of Allshowa refers to Funb (), so that the resources in FUNB () are not collected by GC, so the resources in Funa () will not.
3.2.2 Example 2: A parameter closure function
function Funa (arg1,arg2) { var i = 0; function Funb (step) { i = i + step; Alert (i) } return FUNB;} var Allshowa = Funa (2, 3); The call is Funa Arg1=2,arg2=3allshowa (1);//Call is Funb step=1, Output 1allShowA (3);//Call is Funb setp=3, output 4
3.2.3 Example 3: variable sharing within the parent function Funa
function Funa () { var i = 0; function Funb () { i++; Alert (i) } ALLSHOWC = function () {//ALLSHOWC reference anonymous function, shared variable i i++ with Funb ; Alert (i) } return FUNB;} var Allshowa = Funa (); var allshowb = Funa ();//allshowb references FUNA,ALLSHOWC internally rebinding, shared variable i with ALLSHOWB
3.3 Applicable Scenarios
① guarantees that the variables inside the function Funa are safe, because external variables cannot be accessed directly from the Funa.
================================== Series article ==========================================
This article: 3.2 JavaScript function functions category
Web Development Road Series articles
Types of JavaScript function functions