26 ms·
A lexical closure is similar in many ways to the class of an object-oriented language. In fact, lexical closures can be used to create an object system. A clas
by Ixiaus 17y ago
A lexical closure is similar in many ways to the class of an object-oriented language. In fact, lexical closures can be used to create an object system.
A class in PHP, for example, can have any number of class variables and for this demonstration consider these variables to be "private" (not accessible outside of the class or its own instances). One can, however, write methods or functions in the class that can provide an interface to operate on or access these private class variables.
That is, essentially, what a lexical closure is but without the overhead of an object system (remember, an object system can be created using closures!). They are a remarkably useful pattern of encapsulating certain kinds of logic.
Continuations, are another fun topic related to lexical closures too...