Functions carry the ability to retain and access memory within their respective scopes. This legality, in combination with the capacity to return functions, allow for rather interesting code implementations. In the code snippet below, after instance has been called, every function call thereafter results in a return of “after.” Note that due to the lexical nature of JavaScript, the returned function retains access to the scope of the returnOnce function (and therefore the alreadyCalled variable).
Once this concept of scoping is understood, we may craft considerably more clever functions. In the example below, we take in some unspecified number of functions as arguments. These functions are then saved and set to be applied to the variable parameter at some unspecified time in the future.

