Nested Functions
An under-utiilzed but interesting use of functions are as nested functions, functions that are essentially not global but privately embedded within another function. Sure you can have functions declared as private, but that will only protected the function from being accessed by another class.
Protected from external access, nested functions can be called and used by their enclosing function, which can also return the nested function. I see nested functions as providing an encapsulated convenience like closures, allowing you to complete your function within the function rather than chasing the calls across the class. In the following code, the nested function is only accessible within it’s parent or enclosing function:
https://gist.github.com/doronkatz/f2ec5901b46f325032ad.js
Discover more from Doron Katz
Subscribe to get the latest posts sent to your email.