The _.mixin
function from the Underscore library allows you to add your own functions to the Underscore library in a way that allows them to be chained like regular Underscore functions. Here is an example of how to use it:
index.tsx482 chars17 lines
In the above example, we define a custom function double
that doubles a given number. We then add this function to the Underscore library using the _.mixin
function. This function takes an object as its parameter, where each key-value pair represents the name of the function being added and the function itself. Since we only want to add one function in this example, we pass a single object literal with the double
function as its value.
Once we have added our custom function to Underscore, we can use it like any other Underscore function. In this example, we chain the _.map
function to our array of numbers, first mapping each number to its double and then adding one to the result. The final result is [3, 5, 7].
gistlibby LogSnag