Let's say that I want to create a function that trims its input, one way I can do it is simply
x => x.trim()
Another way would be
x => String.prototype.trim.call(x)
Which has the advantage that if x
has defined an override for the .trim()
, I can still grab the implementation from the prototype. By extension, if I want to use the same things in a .map()
I can do either,
[" foo", "bar "].map(x => x.trim())
[" foo", "bar "].map(x => String.prototype.trim.call(x));
I wante do make a pointfree function though. What I don't understand is why can't use a uninvoked function created with .call
[" foo", "bar "].map(String.prototype.trim.call);
I also tried .apply
which because there are no arguments is effectively the same thing.
Likewise, outside of a map you also can't use the function .call
function
> fn = String.prototype.trim.call
[Function: call]
> fn('asdf')
TypeError: fn is not a function
So my questions are,
- In the above, the function returned by
String.prototype.trim.call
does what exactly? What kind of function is that? - Why doesn't my attempt with
.call
work? - Is there anyway to do this with
.bind
,.apply
, or.call
?