For some reason I can't use String.prototype.trim.call
as a callback for array methods, such as map
or filter
.
In this case, two functions work the same:
function trim(string) {
return string.trim();
}
var string = ' A ';
trim(string); // 'A'
String.prototype.trim.call(string); // 'A'
However, when I try to pass them as a callback for an array method, 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
I assume in a latter case that this
doesn't point to an array element, but I would like to get clear explanation of what's happening.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…