Closures Taco-Truck

So what is closure in javascript?

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

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

One use for closures are creation of independent Instances of a specific algorithm.

For more documentation: MDN  —  medium  — JS-Blog

 

Example:

Taco-Truck

This is “Jennifer“, she grab her saivings and purchase a Taco-Truck, also hire two employees to run the enterprise: “Elena & Maria“, and some times her 17 year old brother “Hector” works few days, what you don’t know is that Jennifer also likes JavaScript and she decide to create a program to do the payroll using “JavaScript Closures

Elena and Maria both get paid $10.00 USD per hour and Hector $8.00, to keep the quality, and customer satisfaction HIGH she has a FaceBook page where costumes can give likes to the employee that give the best service, and whoever gets the most likes gets a $15.00 USD Bonus to be pay with next week check.

with the previous information “Jennifer” wrote the next code:

Then open the console and play with the following commands:

  • check;       -to see the function’s
  • maria();    -to run the “maria” instance of the function.
  • elena()      -to run the “elena” instance of the function.
  • console.dir(elena);    -to analize the current state of the “elena” instance function, and its variables (closures).

Here is an example of some of the commands:

There you will be able to see how you can create a new instance for each employee, also you can analize the current variables values by using the console.dir(elena); command. those variables are totally independent of each other, and they not get affected when another instances is modify.

Other Applications for CLOSURES:

Here is a real life example where a closures to update the DOM: JsBin

 

Articles & Examples from the Internet:

Articles with info in Closures: Bov  — medium  —  JERF

Example 1:

Example 2:

In the JavaScript (or any ECMAScript) language, in particular, closures are useful in hiding the implementation of functionality while still revealing the interface.

For example, imagine you are writing a class of date utility methods and you want to allow users to lookup weekday names by index but you don’t want them to be able to modify the array of names you use under the hood.

Note that the days array could simply be stored as a property of the dateUtil object but then it would be visible to users of the script and they could even change it if they wanted, without even needing your source code. However, since it’s enclosed by the anonymous function which returns the date lookup function it is only accessible by the lookup function so it is now tamper-proof.

 

x



Copyright 2017. All rights reserved.

Posted February 16, 2017 by Edmundo in category "JS