Home >Common Problem >What is closure
What is a closure?
In computer science, closure (English: Closure), also known as lexical closure (Lexical Closure) or function closure (function closures), is a function that references free variables. The referenced free variable will remain with the function even after it has left the environment in which it was created.
So, there is another way of saying that a closure is an entity composed of a function and its associated reference environment. Closures can have multiple instances at runtime, and different reference environments and the same function combination can produce different instances.
The concept of closure appeared in the 1960s, and the earliest programming language to implement closure was Scheme. Since then, closures have been widely used in functional programming languages such as ML and LISP. Many imperative programming languages have also begun to support closures.
In some languages, when another function can be (nested) defined in a function, if the inner function refers to the variables of the outer function, a closure may occur. At runtime, once the outer function is executed, a closure is formed that contains the code of the inner function and references to the variables in the required outer function. The variable referenced is called the upvalue.
The term closure is often confused with anonymous functions. This may be because the two are often used together, but they are different concepts.
The above is the detailed content of What is closure. For more information, please follow other related articles on the PHP Chinese website!