最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't use String#trim as a callback for Array#map - Stack Overflow

programmeradmin0浏览0评论

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: ,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.

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./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.

Share Improve this question edited Mar 11, 2015 at 12:06 Pavlo asked Jan 17, 2014 at 12:47 PavloPavlo 45k14 gold badges83 silver badges114 bronze badges 4
  • exact duplicate of Javascript - Apply trim function to each string in an array - sorry that I've answered twice now :-) – Bergi Commented Jan 17, 2014 at 13:03
  • @Bergi looks like it is, sorry for that. But, as you can imagine, it's almost impossible to find one (I've honestly spend required 15 minutes). I've tried to make the title as general and accurate as possible. – Pavlo Commented Jan 17, 2014 at 13:08
  • Also an elegant solution to this problem would be ES6 arrow function: array.map(s => s.trim());. – Pavlo Commented Jan 17, 2014 at 13:14
  • 1 Yeah, when encountering the question, I knew it was a duplicate but could not find it. So I wrote an answer only to discover the old one thereafter :-) – Bergi Commented Jan 17, 2014 at 13:25
Add a ment  | 

1 Answer 1

Reset to default 10
String.prototype.trim.call(string); // 'A'
array.map(String.prototype.trim.call); // TypeError: undefined is not a function

When you invoke the call method in the first case, its this value is bound to the String.prototype.trim function. In the second case, you just access the call function without having it bound to anything - you could just have used

array.map(Function.prototype.call)

This method is getting invoked with nothing as the this value, the element from your array, the index and the whole array as parameters. When you call call not on a function, it throws. You can either use the second parameter of map or the bind method to fix the this value for the call:

array.map(Function.prototype.call, String.prototype.trim)
array.map(Function.prototype.call.bind(String.prototype.trim))
发布评论

评论列表(0)

  1. 暂无评论