JavaScript-Interview questions: Why use closures?

Source: Internet
Author: User
Look at this answer doesn't seem to understand very thoroughly AH

I'm a Java-learning. Today in the interview when the interviewer mentions Anonymous class, I said Java8 inside provides Lamada type, in JS also has the concept of closure. Interviewer asks what is closure? Why do you use closures?

    • I said: To control access AH. The interior can be accessed externally, but external cannot be accessed internally.

The interviewer did not seem to be satisfied with the answer. Do you have any idea what the great God thinks about this question?

Reply content:

Look at this answer doesn't seem to understand very thoroughly AH

I'm a Java-learning. Today in the interview when the interviewer mentions Anonymous class, I said Java8 inside provides Lamada type, in JS also has the concept of closure. Interviewer asks what is closure? Why do you use closures?

    • I said: To control access AH. The interior can be accessed externally, but external cannot be accessed internally.

The interviewer did not seem to be satisfied with the answer. Do you have any idea what the great God thinks about this question?

Closures, as the name implies, is steamed buns into buns ~

Steamed buns are all flour, the stuffing on the bag becomes a bun

Bun is a steamed bun with stuffing

Closures are functions with their own running environment

Fat brother is a man with background music ~

There are children's shoes do not understand the "self-running environment" meaning ~

Let's say one more time ~

Yard farmers have eaten instant noodles ~
What is the difference between it and ordinary noodles?
is to bring your own seasoning bag.
The seasoning bag is the cooking environment of instant noodles.
It simplifies the process of cooking noodles. So that users do not have to cook and eat delicious inside the cow face.

Functional programming of the closure, is the function of the seasoning package.
User- friendly calling function. You don't have to worry about maintaining a complicated external state.
Python, for example, plays a lot of tricks on closures:
Static Private Variables ~
Partial function ~
Single-Parameter ~
Adorner ~
......

When you are using these features, you are eating "instant noodles" that others have set up a seasoning package.

In fact, I understand that the purpose of closure is to enlarge the scope of the variable.

What is the relationship between executing functions and closures immediately?

Focus on the understanding of the border town, my answer is relatively thin, if still interested, you can look at the scope of the chain Gao Cheng, closure of this section of the exposition ~

Just read an article in the history of the most scolded programming language--javascript

The landlord's answer is not accurate, if I am the interviewer I am not satisfied.
In a nutshell, closures are when a function is returned as an object, and if an external variable is attached, a closure is formed. I very much agree with the analogy of the steamed bread and stuffing into steamed bun classmate's answer, although he is the majority of ridicule, but the degree of understanding is very profound.

If a function is packaged with an external variable, you can give the program a lot of flexibility, you can think of the closure as a lightweight interface encapsulation, although the external function (call mode unchanged), but because the variables are different, you can do many functions. This is what the classmate said comes with the function of running environment, the man with background music, think is terrible.

If you want to know a little bit more, you can refer to one of my articles summarizing the closures in Python, although the programming language is different, but the truth is the same.

Your answer is on the scope of the answer, not about closures. And this answer is also a non-rigorous answer. You simply cannot explain what is inside and what is outside.

I think closures are like this. When a function is called outside the scope where it is defined, it still accesses the scope when it is defined. This phenomenon is called closures.

There are many specific uses, such as the creation of private properties, function currying and so on.

------------Split Line------------

Let me add that the essence of closures is static scope. Because JavaScript does not have a dynamic scope, the function accesses the scope of the definition, so the closure is implemented.

The other answer is that the closure is a function that comes with a running environment. But is it true that any function in JavaScript is not a function that comes with a running environment? Some people therefore think that all functions are closures. It's certainly not a mistake, but it doesn't really make sense to understand closures. Because you normally use functions like this, even if you don't know what a closure is, there's nothing wrong with it. But you may not realize that the global scope is a large closure.

Our common form of closure is a function set B function, then a function returns a B function, so that the B function is executed outside the a function, the scope of the A function can still be accessed. The true strength of closures is reflected in the fact that "B functions are executed in places other than a function".

In summary, closures are just a programming technique based on static scopes. From the interview point of view, you have to answer what is a closure, you first have to explain what the characteristics of the static scope, and then you have to emphasize that "B function in place outside a function," This is a complete answer to the closure.

Lambda calculus only allows single-input single output, so lambda a, b: a + b it is equal to lambda a: lambda b: a + b currying.

In a nutshell, a closure is defined as a function that accesses its scope when it is defined.

So, you say the access control and so on, just a closure of the application scenario, of course, did not answer what is the problem of closures, the interviewer is naturally not satisfied.

In addition, Java in fact does not support closures, anonymous inner class looks similar to the closure, but in fact, whether functional or implementation level is not considered closures. Because:

    1. Functionally, the variables accessed inside the anonymous inner class must be final (Java8 implicit declaration final)

    2. On the implementation, the value of the final variable accessed in the anonymous inner class is actually copied from the outside, so it does not actually have access to the previous scope, which is why it must be final.

A "closure" refers to an expression (usually a function) that has many variables and an environment that binds them, and therefore these variables are also part of the expression.
It's best to create a closure in JavaScript to explain closures:

function a(){var i=0;function b(){alert(++i);}return b;}var c=a();c();

Function b is nested inside function A, and function a returns function B.
So after executing var c=a (), the variable C actually points to function B, and then C () pops up a window showing the value of I (first 1). This code actually creates a closure because the variable C outside of function A refers to function B in function A, which means that when function A's intrinsic B is referenced by a variable outside of function A, a closure is created.

In fact, according to the general form of closure, the simple is that the function inside and nested functions. In team development, in order to prevent naming conflicts, we generally wrap the corresponding code in the form of closures to avoid exposure to the global scope. But the downside is that its internal variables are not immediately recoverable, and there is a risk of memory overflow.

  • 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.