Reprint-Read BLOG-notes-JS in closure (closure)

Source: Internet
Author: User

Original blog title: JS in the closed package (closure) original Blog address: http://www.cnblogs.com/jingwhale/p/4574792.html

The notes are as follows:

1. When declaring variables inside a function, be sure to use the var command. If not, a global variable is actually declared.

function outer () {     localval =;     return localval;} Outer ();
alert (localval);//30

2. The JavaScript language-specific "chain-scoped" structure (chain scope), the child object will look up the variables of all the parent objects first-level. Therefore, all the variables of the parent object are visible to the child object, and vice versa.

For example, if function B is defined within function A, all local variables in a can be accessed by B, whereas all local variables a in B are inaccessible.

3. Simply put, closures (closure) are functions that can read the internal (local) variables of other functions

Since in JavaScript, only sub-functions inside functions can read local variables, closures can be simply understood as "functions defined inside a function". So, in essence, a closure is a bridge that connects the inside of the function to the outside of the function.

4. Function definitions in JavaScript

There are two common ways to define functions: function declarations, function expressions

function declaration: Functions functionname () {  } function expression: var functionname = function () {} You can use a function expression to create a function and execute it immediately: (function () {  var A, b//local variables  //...      and the Code}) ()

The JavaScript parser handles two different ways:

For function declarations, the JS parser takes precedence over the read, ensuring that declarations have been resolved before all code is executed;

For a function expression, like defining a variable of another primitive type, it is parsed only when it executes to a sentence;

So in practice, the difference table that handles two kinds of function definitions now, when you define a function using the form of a function declaration, you can write the call statement before the function declaration, and when you use a function that is defined by a function expression, this will cause an error.

5. Several development scenarios for using closures

Use self-executing anonymous functions to simulate block-level scopes, thereby restricting the addition of too many variables and functions to the global scope to affect the global scope: (function () {        //This is a block-level scope}); Loop closure: function Showallnum (aLi) { For    (var i =0,len = ali.length; i<len;i++) {            Ali[i].onclick = function () {            alert (i);//all is ali.length!        }    }} Clicking will always pop up the same value ali.length instead of 123. The loop has ended before the click, and the I value is ali.length. Rewrite the following function Showallnum (aLi) {for    (var i =0,len = ali.length; i<len;i++) {            Ali[i].onclick = (function (i) {
   
    return function () {                    alert (i);                }            }) (i);    }} Package: var info = (function () {    var _userid = 23492;    var _typeid = ' item ';    function GetUserId () {        alert (_userid);    }    function GetTypeId () {        alert (_typeid);    }}) (); Info.getuserid ();//23492info.gettypeid ();//iteminfo._userid//undefinedinfo._typeid//undefined
   

6. The This in JavaScript

About the this in JS, remember who called, this is pointing to who; to access the closure of this, define a variable to cache. Generally like var _this = this.

7. Before IE9, JScript objects and COM objects use different garbage collection routines, then closures can cause problems. --Memory leaks

8. The principle of closure is described in the original blog

Reprint-Read BLOG-notes-JS in closure (closure)

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.