Home > Backend Development > PHP Tutorial > javascript - Interview question: Why use closures?

javascript - Interview question: Why use closures?

WBOY
Release: 2023-03-01 20:10:01
Original
1576 people have browsed it

After reading this answer, it seems that I don’t understand it very thoroughly

I am learning Java. During the interview today, the interviewer mentioned anonymous classes. I said that Java 8 provides Lamada style, and there is also the concept of closure in JS. The interviewer asked What is closure? Why use closures?

  • I said: Used to control access. The inside can access the outside, but the outside cannot access the inside.

The interviewer seemed not very satisfied with this answer. I wonder what the experts think of this issue?

Reply content:

After reading this answer, it seems that I don’t understand it very thoroughly

I am learning Java. During the interview today, the interviewer mentioned anonymous classes. I said that Java 8 provides Lamada style, and there is also the concept of closure in JS. The interviewer asked What is closure? Why use closures?

  • I said: Used to control access. The inside can access the outside, but the outside cannot access the inside.

The interviewer seems not very satisfied with this answer. I wonder what the experts think of this issue?

Closure, as the name suggests, is to turn steamed buns into steamed buns~

Steamed buns are made entirely of flour, and when filled with fillings they become steamed buns

Baozi is a steamed bun with fillings

Closure is a function that comes with its own operating environment

Fa Ge is a man who brings his own background music~


Some children don’t understand the meaning of “bringing own operating environment”~

Let’s give an example again~

You programmers have all eaten instant noodles, right~
What is the difference between it and ordinary noodles?
Just bring your own seasoning packet.
The seasoning pack is the cooking environment for instant noodles.
It simplifies the process of cooking noodles. This allows users to enjoy delicious beef noodles without having to master cooking skills.

The closure of functional programming is the seasoning package of the function.
Convenient for users to call functions. No need to worry about maintaining complicated external state.
For example, python uses closures in many ways:
Static private variables~
Partial functions~
Single parameterization~
Decorator~
...

When you use these functions, you are actually eating "instant noodles" with seasoning packages set by others.

In fact, my understanding: The purpose of closure is to expand the scope of variables.

What is the relationship between immediate execution of functions and closures

Focus on @biancheng's understanding. My answer is relatively weak. If you are still interested, you can read the explanation of scope chain and closure by Height Three~

An article I just read: The most scolded programming language in history - JavaScript

The author’s answer is not accurate, and I would not be satisfied if I were the interviewer.
Simply put, closure means that when a function is returned as an object, a closure is formed if external variables are entrained. I very much agree with the answer of the classmate who compared steamed buns with fillings to become steamed buns. Although he was mostly joking, his understanding was very profound.

If a function packages external variables, it can give the program a lot of flexibility. You can understand the closure as a lightweight interface encapsulation. Although it is the same function to the outside world (the calling method remains the same), because If the variables are different, many functions can be completed. This is what the classmate said is a function with its own operating environment and a man with his own background music. It’s scary to think about it.

If you want to learn more about it, you can refer to an article I summarized to explain closures in Python in detail. Although the programming languages ​​are different, the principles are the same.

Your answer is about scope, not closure. And this answer is also an imprecise answer. You can't explain what is internal and what is external at all.

I think closures are like this. When a function is called outside the scope in which it is defined, it still accesses the scope in which it was defined. This phenomenon is called closure.

There are many specific uses, common ones include creating private properties, function currying, etc.

------------Separating line------------

I would like to add that the essence of closure is static scope. Because JavaScript does not have dynamic scope, functions access the scope when they are defined, so closures can be implemented.

Other answers say that closures are functions that come with their own operating environment. But in fact, isn’t any function in JavaScript a function that comes with its own running environment? Some people also believe that all functions are closures. This is certainly not wrong, but it actually doesn’t make much sense for understanding closures. Because you usually use functions this way, even if you don't know what a closure is, there won't be any problems. It's just that you may not realize that the global scope is a big closure.

Our common closure form is that function a covers function b, and then function a returns function b, so that when function b is executed somewhere other than function a, it can still access the scope of function a. The point "when function b is executed somewhere other than function a" reflects the real power of closure.

In short, closure is just a programming technique based on static scope. From an interview perspective, if you want to answer what a closure is, you must first explain the characteristics of static scope, and then you must emphasize the point that "when function b is executed somewhere other than function a", it is considered closure. Bao's complete answer.

lambda calculus only allows single input and single output, so lambda a, b: a + b is equal to lambda a: lambda b: a + b, which is currying.

Simply put, the definition of closure is: a function can access the scope when it is defined.

So, the access control you mentioned is just an application scenario of closures. Of course, the interviewer was naturally dissatisfied if he did not answer the question of what closures are.

In addition, Java does not actually support closures. Anonymous inner classes look similar to closures, but in fact they cannot be considered closures in terms of functionality or implementation. Because:

  1. Functionally, variables accessed in anonymous inner classes must be final (Java8 implicitly declares final)

  2. In terms of implementation, the final variable value accessed in the anonymous inner class is actually copied from the outside, so it cannot really access the previous scope, which is why it must be final.

The so-called "closure" refers to an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also part of the expression.
Create a closure in Javascript There is no better way to explain closures than:

<code>function a(){
var i=0;
function b(){
alert(++i);
}
return b;
}
var c=a();
c();</code>
Copy after login

Function b is nested inside function a; function a returns function b.
In this way, after executing var c=a(), variable c actually points to function b. After executing c() again, a window will pop up to display the value of i (the first time is 1). This code actually creates a closure, because the variable c outside function a refers to the function b inside function a. That is to say: when the internal function b of function a is referenced by a variable outside function a, then A closure is created.

In fact, according to the general writing form of closure, simply speaking, it is a function nested inside a function. In team development, in order to prevent naming conflicts, we usually wrap the corresponding code in a closure form to avoid being exposed to the global scope. But the disadvantage is that its internal variables will not be recycled immediately, and there is a risk of memory overflow.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template