Javascript-interview questions: why should we use closures?

Source: Internet
Author: User
After reading this answer, I didn't seem very familiar with it. I learned Java. During the interview today, the interviewer mentioned the anonymous class. I said the Lamada style is provided in Java 8, and the closure concept is also included in JS. What is a closure? Why use a closure? I said: to control access .... It seems that I have not understood this answer thoroughly.

I learned Java. During the interview today, the interviewer mentioned the anonymous class. I said the Lamada style is provided in Java 8, and the closure concept is also included in JS. Interviewer's questionWhat is a closure? Why use a closure?

  • I said: it is used to control access. Internal access to the outside, but external access to the inside.

The interviewer does not seem very satisfied with the answer. I wonder what do you think of this issue?

Reply content:

It seems that I have not understood this answer thoroughly.

I learned Java. During the interview today, the interviewer mentioned the anonymous class. I said the Lamada style is provided in Java 8, and the closure concept is also included in JS. Interviewer's questionWhat is a closure? Why use a closure?

  • I said: it is used to control access. Internal access to the outside, but external access to the inside.

The interviewer does not seem very satisfied with the answer. I wonder what do you think of this issue?

Closure, as its name implies, is to turn steamed buns into steamed buns ~

Steamed bread is full of flour, and it becomes a steamed bread.

Steamed buns are steamed buns with stuffing

Closure is a function with built-in runtime environment

Brother FA is a man with background music ~

Some children's shoes do not understand the meaning of "built-in runtime environment ~

Let's give another example ~

Coders have all eaten instant noodles ~
What is the difference between it and common noodles?
It is the built-in seasoning package.
The seasoning package is the cooking environment for instant noodles.
It simplifies the noodle cooking process. This allows users to enjoy delicious cooking without having to practice.

The closure of functional programming is the functional seasoning package.
ConvenientThe user calls the function. You don't have to worry about maintaining complicated external states.
For example, in python, we have made a lot of tricks on closures:
Static private variable ~
Partial function ~
Single parameter ~
Decorator ~
......

When using these features, you are actually eating the "instant noodles" set by others ".

In fact, I understand that closures are used to expand the scope of variables.

What is the relationship between immediate function execution and closure?

Let's take a look at @ Bian Cheng's understanding. my answer is relatively thin. if you are still interested, you can refer to the description of Scope chain and closure in elevation 3 ~

JavaScript, the most popular programming language in the history of an article I just read.

The answer from the landlord is not accurate. if I am an interviewer, I am not satisfied.
To put it simply, a closure refers to a closure formed when a function is returned as an object with external variables. I strongly agree with the answer from the classmate who added steamed buns and converted them into steamed buns. although he is a joke, he has a profound understanding.

If a function is packaged with external variables, it can provide great flexibility for the program. you can understand the closure as a lightweight interface encapsulation, although this function is used externally (the call method remains unchanged ), however, many functions can be completed because the variables are different. This is what the classmate said about the built-in runtime environment function, and the man with background music is terrible.

If you want to learn more, refer to an article I have summarized to explain the closures in Python. Although the programming languages are different, the truth is the same.

Your answer is about the scope, not the closure. The answer is not rigorous. You cannot explain what is internal or external.

I think the closure is like this. When a function is called outside the scope of its definition, it still accesses the scope of its definition. This phenomenon is called a closure.

There are many specific purposes. Common examples include creating private attributes, function Keri, and so on.

------------ Split line ------------

I will add that the essence of closure is static scope. Because JavaScript does not have a dynamic scope, function accesses are all defined scopes, so closures can be implemented.

In other words, closures are built-in runtime functions. But in fact, isn't any function in JavaScript a function that comes with a runtime environment? Some people think that all functions are closures. This is certainly not an error, but it is of little significance for understanding closures. Because you usually use functions like this, even if you don't know what a closure is, there won't be any problems. However, you may not realize that the global scope is a large closure.

Our common closure form is function a's set of function B, and function a returns function B, so that function B can still access the scope of function a when executed outside function. The point "when Function B is executed outside function a" reflects the real power of the closure.

In short, closures are just a programming technique based on static scopes. From the interview perspective, to answer what is a closure, you must first explain the characteristics of static scopes, then, we must emphasize the point that "B function is executed outside function a" to fully answer the closure.

Lambda calculus only allows single input and single output, solambda a, b: a + bEquallambda a: lambda b: a + bThat is, currying.

To put it simply, a closure is defined as a function that can access its scope when it is defined.

Therefore, what you said about access control is just an application scenario of closures. of course, the interviewer is naturally not satisfied with the question of closures.

In addition, Java does not support closures. anonymous internal classes seem to be similar to closures, but they are not actually functional or implementation-level closures. Because:

  1. Functionally, the variable accessed in the anonymous internal class must be final (Java 8 implicitly declares final)

  2. In implementation, the final variable value accessed in the anonymous internal class is actually copied from the outside, so it cannot actually access the previous scope, this is why final is required.

A closure refers to an expression (usually a function) with many variables and an environment bound to these variables. Therefore, these variables are part of the expression.
It is better to create a closure in Javascript to explain the closure:

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

Function B is nested in function a. function a returns function B.
In this way, after var c = a () is executed, variable c actually points to function B and then executes c () then a window will pop up showing the I value (the first time is 1 ). This code actually creates a closure, because variable c outside function a references function B in function a, that is: when function a's internal function B is referenced by a variable outside function a, a closure is created.

In fact, according to the general writing form of the closure, simply put, the function is nested in the function. In team development, to prevent name conflicts, we usually wrap the corresponding code in the form of a closure to avoid being exposed to the global scope. However, the downside is that internal variables are not immediately recycled 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.