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.

In JavaScript often there are few ways to do the same thing, so choose wisely if you need to use JS-Closures, the following are some examples of Closures in Action.

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.

OCTO-CATCH

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

Times has bee good and “JS” decide to branch out again and now she has open a Bank. Yes you heard it right, one of those financial institutions where we deposit our money for safe keeping and other reasons, and she decide to use JavaScript Closures to handle the Account Information required in such enterprise.

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.

Wrap-Up

JavaScript Closures is one great tool you should have in your tool box, we already know that this is Not a Silver Bullet but it is a bullet, and under right circumstances you can use it, and Kill IT, just remember that tools are as good as the hands that use them, so we need to keep growing at our craft, and practice.

I hope this article on JS-Closures was of help to you, now is time for a call to ACTION.

  1. Study Closures.
  2. Thanks event organizers.
  3. 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.



Copyright 2017. All rights reserved.

Posted March 8, 2017 by Edmundo in category "JS