Utah JS – CLOSURES
So… What is a Closure?
MDN: Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions ‘remember’ the environment in which they were created.
The way I see it: A closure is an “Individual Instance of Function withing a Function that refer to its own Independent variables” and those are “_Private variables” that belong to this specific instance, those variables do not get Garbage Collected. This means that the closure remembers and have access to “its own instance of: Siblings-Variables and Environment” from where this closure was created, even after the “Outter: Function, variables, & Environment” was Garbage collected.
This closures are Inner Functions that have access to the Outer function, and global variables, the “Inner Closure function” can be “Named” or “Anonymous”.
Whit this information we can say that closures are” New, Secure, and Independient Instances of a Functin withing a Function that can remember the enviroment in which was created. Now lest do a little comparation with the last class of Prototype.
Things to keep in mind when using JS-Closures
- They are secure (Notpads).
- Not a Silver Bullet.
- Heavy on the resources.
Jen Silva Taco-Truck
This example is about “Jen“, she grab her savings and purchase a Taco-Truck, in this enterprise she decide to use the power of JS-Closures to run the Payroll, click the following link to see a website with the live example: Jen Taco-Truck.
This example provide a hypothetical example of one use of the JS-Closures.
For “JS” Jen Silva taco business has been good and now she es branching out, she partner with the hotel HYATT ZIVA down in Puerto Vallarta Mexico, there she will run a side business where the guest of the hotel can Scuba-Dive to Catch Octopus. The following is an explanation of how the business work:
You Purchase a Diving Package:
- 5 Dives.
- 5 Oxygen Tanks, each tank last 1 hour ( 1/3 Rule ).
- Number of Octopus allow to catch on each dive: Variate.
- Morning Catch Free Game (at your own risk).
Here is the official website: OCTO-CATCH.
JS CENTURY BANK
The mechanics are simple, when a new customer desires to open an account an employee uses the company portal to create the account make deposits and print information about the Accounts.
Here is the official website: JS CENTURY BANK.
I hope this article on JS-Closures was of help to you, now is time for a call to ACTION.
- Study Closures.
- Thanks event organizers.
- Add me to LinkedIn: Edmundo Rubio.
All code is Live in the websites and the following: GitHub Repo.
I leave you with a philosophical thought:
Coding is when we take time to find and develop solutions for every day problems, combining the computing power of machines and the far reach of the internet, to create TOOLS that are easily available and a solution to those every day problems.
“First we build tools, then they build us” Marshall McLuhan.