The _.ary()
function from lodash creates a new function that wraps the provided function, but only with the first n arguments. The new function created will have the same context as the original function.
Here's an example of using _.ary()
in JavaScript:
index.tsx159 chars10 lines
In the example above, we first require lodash and define a function add
that takes three arguments and returns their sum. We then use _.ary()
to create a new function addOnlyTwo
that only takes the first two arguments, and we pass add
as the first argument to _.ary()
.
In the last line of the code, we call addOnlyTwo()
and pass three arguments. Since addOnlyTwo()
only takes the first two arguments, it ignores the last argument and returns the sum of the first two arguments, which is 3.
Note that _.ary()
is useful when you want to create a new function with a specific arity (number of arguments it takes) without modifying the original function.
gistlibby LogSnag