Effective JavaScript Item 11 mastering closures

Source: Internet
Author: User

this series as effective JavaScript 's reading notes.

To master closures, you need to know the following key points:

    1. JavaScript allow in the current function accessed in this function the external variable.

function Makesandwich () {var magicingredient = "Peanut butter"; function make (filling) {return magicingredient + "and" + filling;} return make ("jelly");} Makesandwich (); "Peanut Butter and Jelly"


above the Make function access to the external magicingredient variable.

  1. afunctionto access thefunctionoutside of the variable, even if the outerfunctionhas returned. It sounds a little weird, but don't forget toJavaScriptin whichfunctionis aFirst-class Object(seeItem).

function Sandwichmaker () {var magicingredient = "Peanut butter"; function make (filling) {return magicingredient + "and" + filling;} return make;} var f = sandwichmaker (); F ("jelly"); "Peanut Butter and Jelly"


How the above code works:

in fact, JavaScript in the function not only does it record information about the code that needs to be executed, but it also holds information about all the variables it references, which is actually the concept of closures.

then the above makefunction is a closure, it saves the magicingredient as well Filling information for these two variables.

Therefore, this can be used to declare more General-purpose of the function :

function Sandwichmaker (magicingredient) {function make (filling) {return magicingredient + "and" + filling;} return make;} var Hamand = Sandwichmaker ("Ham"); Hamand ("cheese"); "Ham and Cheese" hamand ("mustard"); "Ham and Mustard"

closures are JavaScript The most significant and elegant features in the JavaScript even provides a more concise way to create closures, called Function Expression :


function Sandwichmaker (magicingredient) {return function (filling) {return magicingredient + "and" + Filling;};}

Notice that the above functionexpression is anonymous, because all that is needed here is to return a function ( i.e. closures ) To refer to some information. Of course,function expression can have a name, which is described in Item .

    1. Closures can update external values. In fact, closures simply save a reference to an external value, rather than copying their values.

function box () {var val = undefined;return {set:function (newval) {val = newval;},get:function () {return val;},type: function () {return typeof Val;}};} var b = box (); B.type (); "Undefined" b.set (98.6); B.get (); 98.6b.type (); "Number"

above the Box The function returns an object that contains three closures, each of which is referenced to its outer Val variable, where Set closures can also Val variable to be modified.

Summarize:

    1. function to access variables outside of it.
    2. closures can also be accessed after the function that created them is returned. (Closures can outlive the function that creates them. )
    3. The inside of the closure stores references to the external variables used and is able to get and modify their values.





Effective JavaScript Item 11 mastering closures

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.