请教一个JS函数的作用

function get(arr) {
  return arr.map(
      Function.prototype.call,
      String.prototype.trim
  )
}
// map的第二个参数
console.log(get(['a b', 'u 2 1']));

以上代码是什么意思? 求详解

阅读 1.4k
2 个回答
      function get(arr) {
        return arr.map(function(...args) {
          return String.prototype.trim.call(...args)
        })
      }
      
      console.log(get([' a b ', ' u 2 1 ', 1223, 1212]))
      //["a b", "u 2 1", "1223", "1212"]

why?

String.prototype.trim.call(2) //2
2.trim()//error

对于arr中的每个对象,使用trim作为call的上下文来调用,最后的效果等同于调用每个对象的trim方法

推荐问题