dvas0004 / NerdNotes

A collection of notes: things I'd like to remember while reading technical articles, technical questions I couldn't answer, and so on.
12 stars 0 forks source link

Using .bind for passing initial default arguments #45

Open dvas0004 opened 5 years ago

dvas0004 commented 5 years ago
function addition(x, y) {
   return x + y;
}

const plus5 = addition.bind(null, 5) 
plus5(10) // output -> 15

Note the use of .bind above, which returns another function ("plus5") which has the "x" input by default set to null, and "y" set to 5 by default. This way xalling plus5 with just the "x" variable set (as per last line) won't result in null, because "y" is set by default

dvas0004 commented 5 years ago

https://overflowjs.com/posts/Javascript-Currying-VS-Partial-Application?fbclid=IwAR3Y2Ibbu1snwX0ANnNFi57duu6kaJpyGdMoOU4Okwxxq3neqzltXvbE-Ig