Nugget 5: JavaScript Function Factory and Closure
A function factory in JavaScript is a function that returns an object. For this Nugget, I created a financial portfolio page to illustrate Function Factory and Closure in JavaScript. I had too much fun doing this. FinancialStock () is a function factory that creates an equity stock object. Every time this function is invoked, a new object is created. Note that all objects will have the same prototype; they all have the same properties and the same stockFluctuation() calculation function. When the page is loaded, a new factoryManager() object is created, and when the "Add" button is clicked then the addSymbol () function is invoked and the factoryManager creates a new equity symbol. The Closure When a Closure is created in JavaScript, a function can access values outside of its own curly braces, outside of its scope. This happens every time a new equity symbol is created. Note that createSymbol() has access to the variable indexId which is used as the sym...