javascript - difference between js functions by using parameters -


what's difference between:

// example 1 sum(8,2) console.log(sum(8,2));   // outputs what?? // example 2 sum(8)(2) console.log(sum(8)(2));  // outputs what??  function sum(x,y) { return x+y; }  function sum(x) {    return function(y){      return x+y;      }  } 

why 1 used on other , why?

what trying called function currying

try this:

function sum(x) {     return function(y) { return x + y; }  };  var sumwith4 = sum(4);  var finalval = sumwith4(5); finalval = sumwith4(8); 

one of advantages helps in reusing abstract function. example in above example can reuse sumwith4 add 4 number out calling sum(4,5) explicitly. simple example. there scenarios in part of function evaluated based on first param , other part on second. can create partial function providing first param , reuse partial function repeatedly multiple different second params.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -