But I honestly like the Closure + Closure::fromCallable approach, because string or array as callable has always been weird. Will B. Over a year ago @RoboRobok one reason for requiring only Closure (anonymous function) as opposed to callable, would be to prevent access beyond the scope of the called function.
A closure can actually be any function within another function, and its key characteristic is that it has access to the scope of the parent function including it's variables and methods.
A closure is a pairing of: A function and A reference to that function's outer scope (lexical environment) A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values. Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context ...
A closure allows you to bind variables into a function without passing them as parameters. Decorators which accept parameters are a common use for closures. Closures are a common implementation mechanism for that sort of "function factory". I frequently choose to use closures in the Strategy Pattern when the strategy is modified by data at run ...
Closures are an aspect of lambda expressions. Lambdas need not necessarily support closures. Some languages implement it differently. For eg, Java is different from C# in that former doesn't allow modification of closed over variable within the function. That said I think this question is about general understanding, hence duplicate of What is the difference between a 'closure' and a 'lambda'?
That's the magic, and frustration, of closure. "JavaScript Functions close over the scope they are declared in, and retain access to that scope even as variable values inside of that scope change." Using let instead of var solves this by creating a new scope each time the for loop runs, creating a separated scope for each function to close over.
A closure in C# takes the form of an in-line delegate/ anonymous method. A closure is attached to its parent method meaning that variables defined in parent's method body can be referenced from within the anonymous method.
A closure is a function value created from a possibly nested function declaration or function expression (i.e. lambda expression) whose body contains may one or more references to variables declared in an outer scope.