The partial
function from Underscore allows you to create a new function that has some of the arguments of the original function pre-filled with specific values. This can be very useful when you want to reuse a function with some of the arguments already defined.
Here's an example of how to use partial
:
index.tsx364 chars14 lines
In this example, we define a function multiply
that takes two arguments. Then, we use _.partial
to create a new function called multiplyBy5
that takes a single argument and always multiplies it by 5. We pass multiply
as the first argument to _.partial
and 5
as the second argument, which tells partial
to pre-fill the first argument of multiply
with 5
. Finally, we call multiplyBy5
with the argument 10
and get the expected result of 50
.
You can use _.partial
with any function that has more than one argument, and you can pre-fill as many or as few arguments as you need.
gistlibby LogSnag