Closure
A closure gives you access to an outer function's scope from an inner function. A closure is a function having access to the parent scope, even after the parent function has closed. The closure has access to variable in three scopes:
  • Variable declared in his own scope
  • Variable declared in parent function scope
  • Variable declared in global namespace

Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions.

Closure
var globalVar = '12';

function outerFn(arg1, arg2){
	var outerFuncVar = '23';   
	var myClosureVar = 3;
		
	function innerFn(innerArg){
		var innerFuncVar = '34';
		return arg1 + arg2 + myClosureVar + innerArg;
	}
		
	//return a reference to the inner function defined as - innerFn
	return innerFn;
}	
	
function runMe() {
	var innerFuncVar = "y";
	var refToInnerFn = outerFn(1,2);

	console.log("creating outerFn(1,2)");
	console.log(refToInnerFn(4));				//returns 10 (1+2+3+4)
	console.log(refToInnerFn(5));				//returns 11 (1+2+3+5)
	console.log(refToInnerFn(6));				//returns 12 (1+2+3+6)
		
	var anotherRefToInnerFn = outerFn(11,22);
	console.log("creating outerFn(11, 22)");
	console.log(anotherRefToInnerFn(4));		//returns 40 (11+22+3+4)
	console.log(anotherRefToInnerFn(5));		//returns 41 (11+22+3+5)
	console.log(anotherRefToInnerFn(6));		//returns 42 (11+22+3+6)
}



Ginger CMS
the future of cms, a simple and intuitive content management system ...

ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ...

CFTurbine
cf prototyping engine, generates boilerplate code and views ...

Search Engine LITE
create your own custom search engine for your web site ...

JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ...

Validation Library
complete validation library for your web forms ...