Closures by example JavaScript

by Mohan 2012-09-20 23:10:06


Closures by example
JavaScript 1.5
function CreateAdder( add ) {

return function( value ) {

return value + add;
}
}
usage:
var myAdder5 = CreateAdder( 5 );
var myAdder6 = CreateAdder( 6 );

Print( myAdder5( 2 ) ); // prints 7
Print( myAdder6( 4 ) ); // prints 10



599
like
0
dislike
0
mail
flag

You must LOGIN to add comments