JavaScript Closure Learning Notes

Source: Internet
Author: User

This article is all about the closure of Daniel's view, here is just a summary.

There are two scenarios for closure applications--functions as return values, and functions as parameters.

1 in-depth understanding of JavaScript prototypes and closures

It is very simple to judge whether a variable is not an object. The type of the value type is judged with typeof, and the type of the reference type is judged with instanceof.

Everything inside the object is a property, only attributes, no method. Method is also a property. Because its properties are represented in the form of a key-value pair.

Objects in JavaScript can extend properties arbitrarily.

var obj={    a:10,    b:function (x) {        return this.a+x;    }     c:{        name: ' King ',        year:1999    }     }    

  

var fn = function () {            alert (+);        };        Fn.a = ten;        fn.b = function () {            alert (123);        };        FN.C = {            name: "King",            year:1988        };

All (reference types) are objects, objects are collections of properties

2 learning JavaScript closures (Closure)

The special point of the JavaScript language is that the global variables can be read directly inside the function.

A local variable inside a function cannot be read naturally outside the function.

The JavaScript language-specific "chain-scoped" structure (chain scope), in which child objects 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.

Function F1 () {var n=999;     function F2 () {alert (n);  } return F2;  } var result=f1 (); Result (); 999

Closures are functions that can read other functions ' internal variables.

A closure can be simply understood as "a function defined inside a function".

There are two of the maximum uses, one of the variables that can read the inside of the function mentioned earlier, and the other is to keep the values of these variables in memory at all times.

When a function is called as a function instead of a method, this points to a global object? The This in the function refers to the owner of the call to this function

Study questions is the key!

3 JavaScript closures

There are three types of closure implementations that are now more agreeable

With (obj) {    //Here is the object closure    } (function () {    //function Closure    }) () try{//...} catch (e) {//catch closures but IE does not}

The onclick event of the three nodes will be able to eject the corresponding parameters correctly.

1 use a function closure. var lists = document.getElementsByTagName ("Li"); for (var i=0,l=lists.length; i < L; i++) {  Lists[i].onclick = ( function (i) {//saved in the external function    return functions () {      alert (i);    }  }) (i);} 2 using event proxy var ul = document.getElementsByTagName ("ul") [0];ul.onclick = function () {  var e = arguments[0] | | | window.event ,  target = e.srcelement? e.srcelement:e.target;  if (target.nodeName.toLowerCase () = = "Li") {    alert (Target.id.slice ( -1)  }}}

The slice () method returns the selected element from an existing array. The element that contains the arrayobject from start to end (excluding the element).

If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.

4 JavaScript closures in-depth understanding (closure)

When function A's internal function B is referenced by a variable outside function A, we create a "closure" that we normally call "closures."

5 Understanding Javascript Closures

Too many things to see, mark on it ~

JavaScript Closure Learning Notes

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.