Prototype methods are using this
internally to refer to the object that should get manipulated.
call
and apply
lets you specify the this
value when calling a function.
String.prototype.toLowerCase.call('INPUT'); //input
If you want to bind a this
value to a function so that it's invocation form doesn't matter, you can do it with _.bind
or Function.prototype.bind
.
var lowerCaseInput = String.prototype.toLowerCase.bind('INPUT');
lowerCaseInput(); //input
Now, calling _.chain
on an object will wrap it in an underscore object and returns the wrapper and _.tap
takes the result of the previous function in the chain as it's input. Right here we see it's not going to work since String.prototype.toLowerCase
doesn't even expect arguments.
Not only this, but strings are immutable so you would have to do something like the following:
_.chain({ text: 'INPUT' }).tap(function (obj) {
obj.text = obj.text.toLowerCase();
}).value().text;