Nov
27
2015
By abernal
It is when a local variable from inside a method can be used outside of the method itself.
Accesing bound values after closure
This sample shows how the "obstacle" variable can be used after the method has been closed
function warningMaker(obstacle) { return function(number, location) { alert("Beware! There have been " + obstacle + " sightings in the Cove today!\n" + number + " have been spotted at the " + location + "!"); }; } var killerPenguinAlert = warningMaker("killer penguin"); var snowYetiAlert = warningMaker("snow yeti"); // call the two functions here killerPenguinAlert(6, "Ice Caves"); // Beware! There have been killer penguin sightings in the Cove today! 6 have been spotted at the Ice Caves snowYetiAlert(1, "Blizzard Beach"); // Beware! There have been snow yeti sightings in the Cove today! 1 have been spotted at the Blizzard Beach
Modifying bound values after closure
This sample shows how we can increase a variable within a given function
function buildCoveTicketMaker (transport ) { var passengerNumber = 0 ; return function ( name ) { passengerNumber++; alert("Here is your transportation ticket via the "+transport+".\n"+"Welcome to the Cold Closure Cave, "+ name + "!"+" You are passenger #"+passengerNumber+"."); }; } var getSubmarineTicket = buildCoveTicketMaker("Submarine"); getSubmarineTicket("Mario");