$greet = function($name)
printf("Hello %s\r\n", $name);
Back to our discussion. here, i summarized about js anonymous, closures.
- Any function that is defined as a function expression is technically an anonymous function since there is no definitive way to reference it.
- With no definitive way to reference a function, recursive functions become more complicated.
- Recursive functions should always use arguments.callee to call themselves recursively instead of using the function name, which may change.
Closures are created when anonymous functions are defined inside other functions, allowing the closure access to all of the variables inside of the containing function, as follows:-
- Behind the scenes, the closure ’ s scope chain contains a scope for itself, a scope for the containing function, and the global scope.
- Typically a function ’ s scope and all of its variables are destroyed when the function has finished executing.
- When a closure is returned from that function, its scope remains in memory until the closure no longer exists.
- A function can be created and called immediately, executing the code within it but never leaving a reference to the function.
- This results in all of the variables inside the function being destroyed unless they are specifically set to a variable in the containing scope.
- Public methods that have access to private variables are called privileged methods.
- Privileged methods can be implemented on custom types using the constructor or prototype patterns as well as on singletons by using the module or module - augmentation patterns.
original source from Nicholas C. Zakas (Yahoo's principal front-end engineer)