Definition assignment

Definition

Term:Closures (Aterm from CPEN 400A class)

Parenthetical Definition: The function implements a closure (a record storing function environment) inside itself.

Sentence Definition: The closure is a programming technique implemented in programming languages (especially in JavaScript). This technique is usually implementing as a nested function storing enclosing function with its environment regardless of whether the enclosing function exists.

Expanded Definition:

The term Closure was defined by Peter J. Landin in 1964 when he was using a special machine for evaluating expressions. Nowadays, we use this term refers to those nested function which accesses the parent’s function to capture variables through its closure’ scope. Regardless of the old function returned or no longer exists, you can preserve all the elements of the state that the inner function refers to.

A simple closure example consists of a enclosing function return a child function. The basic framework displayed in the Code below. Inside the parent function, variables, object, other functions referred by closure could be stored in an unmanipulated place in order to protect the data from accidently change. The return function can be nested inside an Object because every function in JavaScript is Object. For example here below is a typical JavaScript closure.

Why Closures are useful?

The closure allows programs to remember the state in Web Applications especially when you have many different handlers construct parts of an object. In real front-end development, when AJAX working for transition, closures are very useful for callbacks in JavaScript by returning the callback function from the parent function. Besides, it is a way to emulate private variables in a complex working environment.

What do you need to be careful of?

Nested functions cannot access the parent function’s this and arguments because “this” is always a function local reference that is set by the caller of the function.

Leave a Reply

Your email address will not be published. Required fields are marked *

*