I understand the concept of closure.

Source: Internet
Author: User

The concept of closures is first available in functional programming languages, such as LISP and Haskell. It is generally a feature of a language that supports functional programming. At present, many imperative languages such as javascript and C # can also implement functional programming. They also have the concept of closures.

The definition of a closure is rather abstract and obscure. The concept of a closure I understand is actually to define a subfunction in the function, through which the value in the parent function can be read. The following uses F # as an example. F # Is A. NET-based functional language developed by Microsoft.

Assume there is a function that calculates the sum of squares of numbers.

The result is as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" 63F12263B1234FBFA34860F1A2AF06DA "border =" 0 "alt =" 63F12263B1234FBFA34860F1A2AF06DA "height =" 78 "src =" http://www.bkjia.com/uploads/allimg/131228/1I4555414-0.jpg "/>

The squaresum function is a closure function that can read the values of square1 and square2.

In fact, this function can be colized. After colized, there is only one parameter, and a squaresum function is returned.

The result is as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" 63F12263B1234FBFA34860F1A2AF06DA [1] "border =" 0 "alt =" California [1] "height =" 78 "src =" http://www.bkjia.com/uploads/allimg/131228/1I455C50-1.jpg "/>

This squaresum function is actually a closure function that can read square1 values. Note the call method of this calculate function. First, calculate parameter 3, return a function squaresum, then squaresum calls parameter 4 again, and returns 25. Therefore, we can see that the square1 parameter was inadvertently used during the second call. If you still don't understand it enough, you can read the following code and split it.

The result is as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; widows: 2; text-transform: none; text-indent: 0px; font: 12px; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb (0, 0); border-top: 0px; cursor: default; border-right: 0px; word-spacing: 0px;-webkit-text-size-adjust: auto "title =" E294FEE129064AF0AC16E5E1DB0FBCEB "border =" 0 "alt =" fill "width =" 292 "height =" 104 "data-inited =" true "src =" http://www.bkjia.com/uploads/allimg/131228/1I4553629-2.jpg "/>

The code execution result shows that calculate is called first. This function calculates the value of square1 as 9 and prints it out. After squaresum is taken as the return value, it is assigned to tmp_squaresum and then exits. Then, call tmp_squaresum.The magic result is that square1 is still 9.This is the function of the closure. Although the main function stops running, because its sub-function needs to use the value in the main function, the function is still in the memory and does not exit immediately.

The programmer does not need to do any special work to keep this local variable. the. NET compiler will automatically process the closure and hold the local variables in the parent function until the garbage collector is used to recycle them. Because local variables are not released, excessive use of closures leads to memory shortage. Therefore, we recommend that you do not use closures unless necessary.

To sum up the closure concept, a subfunction is called in the parent function, and the subfunction can continue to use the local variables of the parent function. But to understand it, you still need to practice it yourself.

Since closures are the first concept in functional programming languages, they allow local variables to be held in the memory, in this way, multiple different objects can be accessed. This is the disguised Implementation of the object-oriented system through closures. I guess this is probably the original intention of implementing the closure in the functional form. Therefore, I personally think that the use of closures in object-oriented languages is of little significance.

The above is my understanding, and there may be errors. Correction is welcome.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/881696

Related Article

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.