Types of JavaScript function functions

Source: Internet
Author: User
Tags javascript array

Turn from: http://www.cnblogs.com/polk6/p/3284839.htmlJavaScript function functions Type

This article mainly introduces the common function, anonymous function, closure function

Directory

1. Common functions: Describes the characteristics of ordinary functions: Overwrite with the same name, arguments object, default return value, and so on.

2. Anonymous functions: Describes the characteristics of anonymous functions: Variable anonymous function, unnamed anonymous function.

3. Closure function: Introduces the characteristics of the closure function.

1. General Function 1.1 Example
123 functionShowName(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.

1234567891011 varn1 = 1; function add(value1) {    return n1 + 1;}alert(add(n1));//调用的是下面的函数,输出:3 function add(value1, value2) {    returnvalue1 + 2;}alert(add(n1));//输出:3

1.3 Arguments Object

Arguments is similar to the params of C #, manipulating mutable parameters: The number of arguments passed in the function is greater than the number of arguments when defined.

1234567 functionshowNames(name) {    alert(name);//张三    for(vari = 0; i < arguments.length; i++) {        alert(arguments[i]);//张三、李四、王五    }}showNames(‘张三‘,‘李四‘,‘王五‘);

1.4 Default return value for function

If the function does not indicate the return value, the default return is ' undefined '

123 functionshowMsg() {}alert(showMsg());//输出: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
12345 //变量匿名函数,左侧可以为变量、事件等varanonymousNormal = function(p1, p2) {    alert(p1+p2);}anonymousNormal(3,6);//输出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
123 (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
1234567891011121314 functionfunA() {    vari = 0;    functionfunB() { //闭包函数funB        i++;        alert(i)    }    returnfunB;}varallShowA = funA(); //全局变量引用:累加输出1,2,3,4等functionpartShowA() {    varshowa = funA();//局部变量引用:只输出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
1234567891011 functionfunA(arg1,arg2) {    vari = 0;    functionfunB(step) {        i = i + step;        alert(i)    }    returnfunB;}var allShowA = funA(2, 3); //调用的是funA arg1=2,arg2=3allShowA(1);//调用的是funB step=1,输出 1allShowA(3);//调用的是funB setp=3,输出 4

3.2.3 Example 3: variable sharing within the parent function Funa
1234567891011121314 functionfunA() {    vari = 0;   functionfunB() {        i++;        alert(i)    }    allShowC = function() {// allShowC引用匿名函数,与funB共享变量i        i++;        alert(i)    }    returnfunB;}varallShowA = funA();varallShowB = funA();//allShowB引用了funA,allShowC在内部重新进行了绑定,与allShowB共享变量i

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

1.HTML

1.1 HTML page source code layout Introduction

1.2 HTML Base Control Introduction

The difference between 1.3 iframe and frameset

1.4 The difference between name, ID, class

1.5 table, form label introduction, and get and post submission methods

2.CSS Cascading Style Sheets

2.1 CSS selectors and how to refer to each style

2.2 CSS HTML element layout and Display Properties

2.3 CSS Float Float Property

2.4 CSS Position Positioning properties

3.JavaScript Introduction

3.1 JavaScript var keyword, the state of the variable, exception handling, naming specification and other introduction

3.2 JavaScript function functions type

3.3 JavaScript Array Object

3.4 JavaScript Date Object

3.5 JavaScript Math and number objects

3.6 JavaScript String Object

3.7 JavaScript Object Objects

3.8 JavaScript Custom Objects

3.9 JavaScript Object Properties Introduction

3.10 JavaScript Development Specification

4.BOM

4.1 HTML BOM Browser Object

4.2 HTML Gets the height width of the screen, browser, page

5.DOM

5.1 HTML DOM Introduction

5.2 HTML DOM Object

5.3 An introduction to the HTML event (i) event

5.4 HTML Event (ii) registration and logoff of events

5.5 HTML Event (iii) event flow and event delegation

5.6 HTML Event (quad) analog event operation

6.html5

6.1 HTML5 Introduction

6.2 HTML5 Semantic Element (i) page structure

6.3 HTML5 semantic Element (ii) text content

6.4 HTML5 INPUT Element new features

6.5 HTML5 Progress and meter controls

It's just a record of what I learned and encountered when I was learning and using Web front-end content.

Reference documents:

1) http://www.w3school.com.cn/

2) "JavaScript authoritative Guide (sixth edition)"

Types of JavaScript function functions (RPM)

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.