Such as:
var foo = function (a, b) { return a + b; };
var bar = function (a, b) { return a * b; };
var fn = _.compose([foo, bar]);
How to understand the fn
?
This will not work.
First off, _.compose()
doesn't take an array. It takes a list of functions as separate arguments as in:
var fn = _.compose(foo, bar);
Then, even with it done that way, with underscore's _.compose()
, each successive function is passed the return result of the previous function. Since both your functions require two arguments and return one, this will not work.
If you fix your code to pass the arguments properly to _.compose()
, then you'd have this:
var foo = function (a, b) { return a + b; };
var bar = function (a, b) { return a * b; };
var fn = _.compose(foo, bar);
and if you then use fn
like this:
fn(2, 5);
it will try to execute:
foo(bar(2, 5));
Breaking this apart, bar(2, 5)
will return 10
, so you would then have the result of calling foo(10)
, but that generates NaN
because the second argument to foo()
is undefined
.
_.compose
is designed for functions beyond the last one in the list that operate on a single argument that is the return value of the prior function in the list.
Actual implementation showing that it returns NaN
: http://jsfiddle.net/jfriend00/xhbcdh9k/
The compose
function takes multiple functions and returns a single function. So you would call it as such: var fn = compose(foo, bar);
You can think of fn
as nothing more than the result of calling bar
then foo
. e.g. bar(foo());
Your functions won't compose, since foo
expects 2 parameters foo(a, b)
, but bar
returns 1 value.
I wrote about compose, maybe it can help you understand it better.
I have a string that is used to display text. It is initially set to null, then is updated throughout a function with status updates. I want to make sure the string is set to the correct status ...
I have a string that is used to display text. It is initially set to null, then is updated throughout a function with status updates. I want to make sure the string is set to the correct status ...
I want to make something like Inspect Element highlight. I want to add a border to divs on mouseover. I don't want this to change the width of the div and also, there are some divs with their own ...
I want to make something like Inspect Element highlight. I want to add a border to divs on mouseover. I don't want this to change the width of the div and also, there are some divs with their own ...
<html> <input type="file" name="Image"> </html> <script> fabric.Image.fromURL('image.jpg', function(img) {enter code here var oImg = img.set({ left: 50, top: 100, angle: 00 })....
<html> <input type="file" name="Image"> </html> <script> fabric.Image.fromURL('image.jpg', function(img) {enter code here var oImg = img.set({ left: 50, top: 100, angle: 00 })....
I have four grids, and in each grid I have a button that I would like to have its own unique modal. Whenever I try using Bootstrap's modals' however, I am only getting the first button's data to show ...
I have four grids, and in each grid I have a button that I would like to have its own unique modal. Whenever I try using Bootstrap's modals' however, I am only getting the first button's data to show ...