For some reason, I cannot use String.prototype.trim.call as a callback for array methods like map or filter .
In this case, the two functions work the same way:
function trim(string) { return string.trim(); } var string = ' A '; trim(string);
However, when I try to pass them as a callback to an array method, the second one fails:
var array = [' A', 'B ', ' C ']; array.map(trim); // ['A', 'B', 'C']; array.map(String.prototype.trim.call); // TypeError: undefined is not a function
Demo: http://jsbin.com/ubUHiHon/1/edit?js,console
In the latter case, I assume that this does not point to an element of the array, but I would like to get a clear explanation of what is happening.
javascript string prototype
Pavlo
source share