A function is an event-driven or reusable code block that can be executed when it is called. Next, let's take a look at the function functions in javascript together with the small Editor, let's take a look at some of the following methods to define functions in JavaScript:
1. The most basic function declaration is used as a local shard.
The Code is as follows:
Function func (){}
Or
The Code is as follows:
Var func = function (){};
2. Use as a class constructor:
function class(){}class.prototype={};var item=new class();
3. Use as a closure:
(Function () {// independent scope })();
4. It can be used as a selector:
Var addEvent = new function () {if (! -[1,]) return function (elem, type, func) {attachEvent (elem, 'on' + type, func) ;}; else return function (elem, type, func) {addEventListener (elem, type, func, false) ;}}; // avoids repeated judgments
5. Hybrid applications in the above four situations:
Var class = new function () {var privateArg; // static private variable function privateMethod = function () {}; // static private method return function () {/* real constructor *
JavaScript function types: describes common functions, anonymous functions, and closure functions.
1. Introduction to common functions
1.1 example
function ShowName(name) { alert(name);}
1.2 coverage of functions with the same name in Js
In Js, functions are not overloaded. functions with the same function name and different parameter signatures are defined. The subsequent functions will overwrite the previous functions. Only the following functions are called.
Var n1 = 1; function add (value1) {return n1 + 1;} alert (add (n1); // call the following function and output it: 3 function add (value1, value2) {return value1 + 2;} alert (add (n1); // output: 3
1.3 arguments object
Arguments is similar to the params of C #. variable parameters: the number of parameters passed in to a function is greater than the number of parameters defined at the time of definition.
Function showNames (name) {alert (name); // Zhang San for (var I = 0; I <arguments. length; I ++) {alert (arguments [I]); // Zhang San, Li Si, Wang Wu} showNames ('zhang san', 'Li si', 'wang wu ');
1.4 default range value of the Function
If the function does not specify the return value, 'undefined' is returned by default'
Function showMsg () {} alert (showMsg (); // output: undefined
2. Anonymous Functions
2.1 variable anonymous Functions
2.1.1 description
You can assign values to variables and events.
2.1.2 example
// Variable anonymous function. The left side can be variables, events, and other var anonymousNormal = function (p1, p2) {alert (p1 + p2);} anonymousNormal (3, 6); // output 9
2.1.3 applicable scenarios
① Avoid function name contamination. If a function with a name is declared first and then assigned to a variable or event, the abuse of the function name is caused.
2.2 anonymous functions without names
2.2.1 description
That is, when the function is declared, the parameter is followed. When the Js syntax parses this function, the code in it is executed immediately.
2.2.2 example
(function (p1) { alert(p1);})(1);
2.2.3 applicable scenarios
① Only one execution is required. If the browser load is complete, you only need to execute the function once and will not execute it later.
3. Closure Functions
3.1 Description
Assume that function A declares function B internally. function B references A variable other than function B, and the return value of function A is A reference of 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: accumulative output of functions such as 1, 2, 4, and 3, partShowA () {var showa = funA (); // local variable reference: Only output 1 showa ();}
AllShowA is a global variable that references the function funA. If you run allShowA () repeatedly, the accumulated values, such as 1, 2, 4, and so on, are output.
Execute the partShowA () function because only the local variable showa is declared internally to reference funA. After the execution is complete, the resources occupied by showa are released due to the scope.
The key to closure is scope: the resources occupied by global variables are released only when the page is changed or the browser is closed. Var allShowA = funA () is equivalent to allShowA referencing funB (), so that the resources in funB () are not recycled by GC, so the resources in funA () are not.
3.2.2 Example 2: A closure function with Parameters
Function funA (arg1, arg2) {var I = 0; function funB (step) {I = I + step; alert (I)} return funB ;} var allShowA = funA (2, 3); // call funA arg1 = 2, arg2 = 3 allShowA (1); // call funB step = 1, output 1 allShowA (3); // call funB setp = 3, output 4
3.2.3 Example 3: Share variables in the parent function funA
Function funA () {var I = 0; function funB () {I ++; alert (I)} allShowC = function () {// allShowC reference anonymous functions, share the variable I ++; alert (I)} return funB;} var allShowA = funA (); var allShowB = funA (); // allShowB references funA, allShowC is rebound internally to share variable I with allShowB
3.3 applicable scenarios
① Ensure the security of the variables in the function funA, because the variables in funA cannot be directly accessed from outside.
The above content is the function in js.