In this post I will be taking a look at the lodash _.over method. This method can be used to create a function that calls an iteratee function with all the arguments that are given to it returns the result. It might prove useful in some situations so lets take a quick look.
The idea here is that if I have more than one method that has uniform arguments, and I want a method that will return and array of results for all of them, then the _.over method can be used to do just that. I just call the _.over method and pass in an array of the methods that I want to be used.
In the above example I have two methods that accept an x and y variable in the same order, one just adds and the other just multiplies them. Passing references to each of these methods in an array to the _.over method results in a new method that when used will return an array of results for each method.
When it comes to making a method for _.over the arguments object might be of interest, as well as closures. I will not be getting into the nature of the arguments object in detail as I have all ready wrote a post on it, same is true of closures. I will mention that the arguments object is useful when you want to design a method that will accept a variable number of arguments.
It is possible to install just a single lodash method by itself, often it seems like there is just that one method here or there when it comes to using lodash these days in conjunction with what is available natively. However it is also of course possible to write, or just copy and paste something like this:
The over method that I slapped together here is a good example of the power of closure. It is a method that returns a method that acts on something that is within another function level variable scope that is created when I call the over method and pass the methods I want it to use. It is also a good example of the Function.apply method as well that comes in handy often as well. If you are not up to speed with Function.call, apply, and bind be sure to check out my post on that, and write some examples, I find myself using them all the time.