I also talk about JavaScript closures

Source: Internet
Author: User

1. What is a closure?
Whenever you see the function keyword within another function, the inner function have access to variables in the outer fun Ction

functionFoo(X) { VarTmp= 3; Return function (y) { (x + y + (++tmp//would also alert 16 }}< Span class= "PLN" >var bar = foo 2//bar is now a closure. bar10
/span>
/** when a function was defined in another function and it* have access to the outer function ' s context even after* The outer function returns.** an important concept to learn in javascript.*/functionOuterfunction(Somenum) { VarSomestring= ' hey! '; VarContent=Document.getElementById(' Content '); function Innerfunction ()  {. InnerHTML = Somenum + : ' 


Sentence summaries:

    • A closure is the local variables for a function-kept alive After the function has returned, or
    • A closure is a stack-frame which was not deallocated when the function returns (as if a ' stack-frame ' were malloc ' Ed instead of being on the stack!).

The following code returns a reference to a function:

functionSayHello2(Name) { VarText= ' Hello ' + Name;//Local variable var Sayalert = function ()   { Alert (text } return Sayalert;}say2 = Sayhello2 ( span class= "str" > ' Bob ' ); say2 (); 

/span>

Most JavaScript programmers would understand how a reference to a function are returned to a variable in the above code. If you don ' t and then you need to before you can learn closures. A C programmer would think of the function as returning a pointer to a function, and that the variables sayAlert and say2 wer e each a pointer to a function.

There is a critical difference between a C pointer to a function and a JavaScript reference to a function. In JavaScript, can think of a function reference variable as have both a pointer to a function as well As a hidden pointer to a closure.

The above code has a closure because the anonymous function function() { alert(text); } was declared inside another function, in sayHello2() t his example. In JavaScript, if your use function of the keyword inside another function, you are creating a closure.

In C, and most other common languages after a function returns, all the local variables is no longer accessible Because the Stack-frame is destroyed.

In JavaScript, if you declare a function within another function and then the local variables can remain accessible after RET Urning from the function you called. This was demonstrated above, because we call the function after say2() we had returned from sayHello2() . Notice that the code that we call references the variable text , which is a local variable of the function sayHello2() .

function() { alert(text); } // Output of say2.toString();

Click the button above to get JavaScript to print out the code for the anonymous function. You can see that the code refers to the variable text. The anonymous function can reference text which holds the value ‘Bob‘ because the local variables of sayHello2() is Kep T in a closure.

The magic is, in JavaScript a function reference also have a secret reference to the closure it was created In-simila R to how delegates is a method pointer plus a secret reference to an object.

            
            

I also talk about JavaScript 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.