📖 WIPIVERSE

🔍 Currently registered entries: 75,968건

Closure (computer programming)

A closure in computer programming is a function bundled together with its surrounding state (lexical environment). This environment consists of any free variables used by the function that are not defined within the function itself. In essence, a closure allows a function to access variables from its enclosing scope, even after the outer function has finished executing.

More specifically, a closure is created when a function is defined inside another function (the outer function), and the inner function references variables from the outer function's scope. When the outer function returns, the inner function (the closure) retains access to those variables, preserving their values even though the outer function is no longer active. This ability to "remember" its lexical environment is a defining characteristic of closures.

Closures provide a powerful mechanism for data encapsulation, state preservation, and code modularity. They enable the creation of functions that behave differently based on the environment in which they were created. This is useful for tasks such as creating private variables, implementing callbacks, and building curried functions.

The concept of closures is found in many programming languages, including JavaScript, Python, Lisp, Scheme, and others, although the specific implementation details may vary. The presence of closures is often associated with languages that support first-class functions (functions that can be treated as variables).