我有一个字符串数组.我想修剪数组中的每个字符串.
[' a', ' b ', 'c'].map(String.prototype.trim);
Run Code Online (Sandbox Code Playgroud)
......但我的控制台说......
TypeError:String.prototype.trim在null或undefined上调用
我在数组中看不到任何null或undefined值.
String.prototype.trim()并Array.prototype.map()在Chrome 17中定义,我正在测试.
为什么这不起作用?我觉得我忽略了一些显而易见的事情.
我意识到我可以在那里循环或删除一个函数.然而,这不是这个问题的重点.
Mrc*_*ief 35
@Slace所说的是正确的解释.@ ThomasEding的答案也有效,但有一个可怕的低效率,它可能在循环中创建函数,这不是一件好事.
另一种做法是(参考这里):
[' a', ' b ', 'c'].map(Function.prototype.call, String.prototype.trim);
// gives ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)
标准的浏览器免责声明:这将工作的地方Function.prototype.call,并String.prototype.trim会工作,为旧的浏览器,你可以很容易地替换trim用填充工具是这样的:
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
Run Code Online (Sandbox Code Playgroud)
更新:Interstingly,虽然这在Chrome中运行速度最快,但ThomasEding的方法在IE10和FF20中的运行速度稍快 - http://jsperf.com/native-trim-vs-regex-trim-vs-mixed
Tho*_*ing 25
那是因为trim没有使用适当的this上下文调用.请记住,它this是在JS中动态绑定的.您必须创建一个包装器以传递trim给正确绑定this:
[' a', ' b ', 'c'].map(function (str) {
return str.trim();
});
Run Code Online (Sandbox Code Playgroud)
Aar*_*ell 19
trim在String原型上,意味着它期望this上下文是字符串的上下文,因为map方法on Array将当前数组项提供为第一个参数,this上下文是全局对象.
| 归档时间: |
|
| 查看次数: |
6084 次 |
| 最近记录: |