_javascript techniques for function functions in JavaScript

Source: Internet
Author: User
Tags closure function definition

Let's start by introducing several methods of function definition functions in javascript:

1. The most basic function declared as a Benbon is used.

Copy Code code as follows:

function func () {}

Or

Copy Code code as follows:

var func=function () {};

2. Use as a class Builder:

function class () {}
class.prototype={};
var item=new class ();

3. Use as closures:

(function () {
//standalone Scope
}) ();

4. 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);}
};/ /Avoid repetition of judgment

5. Mixed applications in the above four situations:

var class=new function () {
var privatearg;//static private variable
function privatemethod=function () {};//static Private method
return function () {/* True constructor *

JavaScript function Functions: The main introduction of ordinary functions, anonymous functions, closure functions

1. General function Introduction

1.1 Example

function ShowName (name) {
 alert (name);
}

Overlay of functions with the same name in 1.2 JS

In JS, the function is not overloaded, the same function name, the signature of the different parameters of the function, the following function will overwrite the previous function. When invoked, only the following functions are called.

var n1 = 1;
function Add (value1) {return
 n1 + 1;
}
Alert (add N1);//called the following function, output: 3
function Add (value1, value2) {return
 value1 + 2;
}
Alert (Add (N1));//output: 3

1.3 Arguments Object

Arguments similar to C # 's params, manipulating variable parameters: The number of arguments passed in a function is greater than the number of parameters defined.

function ShowNames (name) {
 alert (name);//John for
 (var i = 0; i < arguments.length; i++) {
  alert (argument S[i]);//John, Dick, Harry
 }
shownames (' John ', ' Dick ', ' Harry ');

The default range value of the 1.4 function

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 Sample

Variable anonymous function, and the left can be
var anonymousnormal = function (P1, p2) {
 alert (P1+P2) for variables, events, and so on;
}
Anonymousnormal (3,6);//Output 9

2.1.3 Applicable scene

① avoid the pollution of the function name. If you declare a function with a name, and then assign a value to a variable or event, it causes the misuse of the function name.

2.2 Anonymous function without name

2.2.1 Description
That is, when the function declaration is followed by the argument. When the JS syntax resolves this function, the inside code executes immediately.

2.2.2 Sample

(function (p1) {
 alert (p1);
}) (1);

2.2.3 Applicable scene

① only need to be executed once. If the browser finishes loading, just perform the function once and not performed.

3. Closure function

3.1 Description

Suppose, function a declares a function B, function b references 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 References and local references

function Funa () {
 var i = 0;
 function Funb () {//Closure functions Funb
  i++;
  Alert (i)
 } return
 Funb
}
var Allshowa = Funa (); Global variable reference: Cumulative output 1,2,3,4 and other
function Partshowa () {
 var Showa = Funa ();//local variable reference: Output only 1
 Showa ();
}

Allshowa is a global variable that references the function Funa. Running the Allshowa () repeatedly will output the cumulative value of 1,2,3,4.

Executes the function Partshowa (), because only local variable Showa is declared internally to refer to Funa, and after completion, the resources occupied by the Showa are released due to the scope relationship.

The key to closures is scope: the resources that a global variable occupies will only be released when the page transformation or browser is closed. When var Allshowa = Funa (), the equivalent of Allshowa references Funb () so that the resources in FUNB () are not reclaimed by GC, and therefore the resources in Funa () are 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=3
allshowa (1);//called Funb step=1, Output 1
allshowa (3);/call 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 refers to an anonymous function, and FUNB shares the variable i
  i++;
  Alert (i)
 } return
 Funb
}
var Allshowa = Funa ();
var allshowb = Funa ();//allshowb references FUNA,ALLSHOWC is internally rebind, and ALLSHOWB shares variable i

3.3 Applicable Scenarios

① guarantees that the variables inside the function Funa are safe because the external cannot directly access the Funa variables.

The above content is to introduce you to the function functions in JS, I hope you like.

Related Article

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.