Using Underscore.js, I can write the following which returns 42:
_([42, 43]).chain()
.first()
.value()
I have custom function, not part of Underscore.js called double():
function double(value) { return value * 2; };
I would like to be able to call this function in an Underscore chain, as if it was part of Underscore. I would like to write the following, which I'd like to return 84:
_([42, 43]).chain()
.first()
.double()
.value()
This can't work since Underscore doesn't define double(). I could use tap() as in:
_([42, 43]).chain()
.first()
.tap(double)
.value()
This is valid, but tap applies the function to its argument and returns the argument, not the result of the function. So it looks to me like I would need a sort of tap that returns the result of the function applied to its argument. Is there anything like this in Underscore.js? Am I missing something terribly obvious?
doubleis a future reserved word and implementations may throw aSyntaxErrorif it's used as an Identifier.tap(runs a function to modify each item) andmap(runs a function that returns a new item).tap()can be used to modify the wrapped object. Am I missing something? underscorejs.org/#tap