Makeadder example from Ben alman
- by Matrym
In the below, where does b come from? I don't see it being passed in, so how could it be returned?
function lockInFirstArg( fn, a ) {
return function( b ) {
return fn( a, b );
};
}
Link: http://msdn.microsoft.com/en-us/scriptjunkie/gg575560
More complete excerpt (sorry for iPad formatting... No tabs):
// More-general functions.
function add( a, b ) {
return a + b;
}
function multiply( a, b ) {
return a * b;
}
// Relatively flexible more-specific function generator.
function lockInFirstArg( fn, a ) {
return function( b ) {
return fn( a, b );
};
}
var add1 = lockInFirstArg( add, 1 );
add1( 2 ); // 3
add1( 3 ); // 4
add1( 10 ); // 11