In JavaScript, you can use the rearg()
function from the lodash library to create a new function with an order of arguments that is different from the original function.
Here's an example that demonstrates how to use rearg()
:
index.tsx189 chars11 lines
In this example, foo()
is the original function that takes three arguments: a
, b
, and c
. We then use _.rearg()
to create a new function called newFoo()
that has a different order of arguments.
The second argument of _.rearg()
is an array that specifies the new order of arguments for newFoo()
. In this example, [2, 0, 1]
means that the third argument of the original function (c
) should be the first argument of the new function, the first argument of the original function (a
) should be the second argument of the new function, and the second argument of the original function (b
) should be the third argument of the new function.
When we call newFoo('hello', 'world', '!')
, it outputs "world hello !"
, which shows that the arguments have been rearranged according to the specified order.
gistlibby LogSnag