topical media & game development

talk show tell print

#javascript-code-14-currying.js / js



  // A function that generators a new function for adding numbers
  function addGenerator( num ) {
  
      // Return a simple function for adding two numbers
      // with the first number borrowed from the generator
      return function( toAdd ) {
          return num + toAdd
      };
  
  }
  
  // addFive now contains a function that takes one argument,
  // adds five to it, and returns the resulting number
  var addFive = addGenerator( 5 );
  
  // We can see here that the result of the addFive function is 9,
  // when passed an argument of 4
  alert( addFive( 4 ) == 9 );
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.