The relationship between the closure function and the anonymous function

Source: Internet
Author: User

The "function is not a closure" is not a problem in itself. While this reference is common on the web, it is because of the misunderstanding that the Javascript implementation mechanism is not clear about random speculation.

First, a function, whether it's an anonymous function or not, has nothing to do with closures!

What is the so-called closure? On the internet, the cloud in the fog, speak inscrutable, but in fact, as long as a little bit of knowledge of the implementation mechanism of the compiler, you will know that closures are actually very simple things.

Let's take a look at the following code:

function Funca () {    = 1, b = 2;     return FUNCB ();         function FUNCB () {        //  Note that A and B are not defined at all in this function, but can be accessed to        return a + b;    = Funca (); // A will be equal to 3

There are several magical places on the above code:

1, functions can be nested definition (and C + + is not)

2. A nested function can access local variables of its ancestor function

First say nested, in fact, this ability is not magical, the C + + compiler can be modified slightly to support, but so realized, although the function can be nested, practical value or relatively small, because access to the local variables of the upper function. And we know that Javascript nesting functions can be nested layers, and access to the most inner function can also access the top-level function of the variable, very useful!

Speaking of which, what's the relationship with closures?

Closures, which are used to implement this nesting, can also layer up access to variable functionality! Yes, it is a simple compiler trick that allows an intrinsic function to access the variables of the parent function.

Simply put, it is implemented in a way that appends an extra hidden object to each function, which is actually called a closure object, and you don't have to say what his name is, it's an ordinary object! It's nothing magical, it's documenting the list of variables inside this function. And the closure object also holds a reference to a closure object that points to the parent function.

In this way, a chain is formed. When we access a variable in a function, the compiler will first see if there is a variable in this closure, and if not, look up. If found, then use, if you have been looking for the head is not, then prompt error. That's what this is about. But at the end of the day, the closures that were constructed in the way above seem to be fixed, and each time a function is called, a new closure object is created and the call corresponds. So the closure chain is actually created dynamically.

In fact, to be clear about this problem, we have to match a few figures. But the time is limited, I will not elaborate. If you can understand that the best, if you still have doubts, then you will be more slowly in contact with the later on the know.

Finally, explain this code to deepen your understanding

function X () {    return  function () {        //  ...     }}


Note that in X there is an anonymous function returned. It is important to remember that this function is defined in X because it is nested, so its closure object is linked to the closure object of X, so in this anonymous function you can access the variables inside the x. It's really that simple. No matter how many layers you nest, anonymous or famous. I only see the nested relationship when you define, you know the whole chain of the closure chain! The rest is a logical matter.

Recommended two out of the domestic common misunderstanding of the method, one is to see ECMA-262, 5e, which contains all the standardized Javascript algorithm implementation details. The other one is the V8 engine that studies Google. Under the code to compile and debug yourself, a lot of problems can be made clear.

The relationship between the closure function and the anonymous function

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.