What is a closure?

Source: Internet
Author: User

First of all, I think it is not necessary to understand or learn a concept if it is not understood or affected. Closure is such a concept that you can use well without understanding it. I have been writing as3 programs in the past two years. I have been dealing with as3 programs every day. I have even had one function set, and one method has more than 20 function extreme examples, but I have never studied how it is implemented. Like water and air, we don't need to know that water is H2O, Air is a mixture of oxygen, nitrogen, carbon dioxide, and so on.

Secondly, I think the online explanation of the closure concept is too narrow, and it seems like I ++ and I are back. If you have to understand this concept and understand it like that, the gains are too small to be worth it.

Wikipedia's interpretation of closures is classic:

In computer science, closure is short for lexical closure, a function that references free variables. This referenced free variable will exist with this function, even if it has left the environment where it was created. Therefore, there is another saying that a closure is an entity composed of a function and its reference environment.

Peter J. landin defined the term closure as an entity that contains environmental and control components in 1964.

Below are the closure concepts I understand.

First look at the closure in mathematics.

() Is an interval. However, when analyzing and calculating this interval, we often use values 1 and 5 that do not belong to this interval. [] is).

In our daily lives, we need to go to department a and department a to create a chapter for department B. Department B said, you must first build a chapter for Department C, department C said that this is not our function scope ...... Play the ball. This is a non-closure. Closure is responsible for the end. If you find Department A, the person who receives Department A is responsible for the end, and he/she coordinates department B and Department C.

In the project, closure is the project manager responsible for scheduling the resources required by the project. If there is anything for the boss and the customer, you can directly find the project manager without looking for other people.

In programming languages, closure is a syntactic sugar. It automatically packs all the resources involved in our purpose and our purpose in a natural form, use it in a natural way that is as easy as possible to avoid misunderstanding. As for its specific implementation, I personally agree that, without affecting the use of the service, I will not provide a thorough explanation. In many cases, you need to access external local variables in a piece of code. If this syntax sugar is not provided, you need to write a lot of code. With the syntax sugar of closure, you don't need to write so much code, so you can use it naturally.

In this way, the closure can be upgraded from a syntax mechanism to a design principle:

Closure is a design concept from the user's point of view. Based on context analysis, it does everything that involves complex things and external environment interaction, it leaves users with a natural interface.

Under this principle, in Functional Languages, the so-called closure is just a "closure", and there are a large number of other types of "closure" waiting for discovery and implementation.

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.