Makeadder example from Ben alman

Posted by Matrym on Stack Overflow See other posts from Stack Overflow or by Matrym
Published on 2011-01-15T09:50:52Z Indexed on 2011/01/15 10:53 UTC
Read the original article Hit count: 616

Filed under:

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

© Stack Overflow or respective owner

Related posts about JavaScript