小编Gre*_*ith的帖子

为什么不["A","B","C"].map(String.prototype.toLowerCase.call)有效吗?

当然,这会返回您的期望:

["A","B","C"].map(function (x) {
    return x.toLowerCase();
});
// --> ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)

使用String.prototype.toLowerCase.call也是如此:

["A","B","C"].map(function (x) {
    return String.prototype.toLowerCase.call(x);
});
// --> ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)

如果你传递map给出的额外参数,它也会起作用,因为它抛弃了参数:

["A","B","C"].map(function (x, index, arr) {
    return String.prototype.toLowerCase.call(x, index, arr);
});
// --> ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用:

["A","B","C"].map(String.prototype.toLowerCase.call);
// --> TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

以下也不起作用,因为arguments具有Object原型而不是Array原型,因此slice未定义它.上述行为的原因可能是因为这样的事情 - 在slice内部使用哪里或其他类似的Array函数?

["A","B","C"].map(function (x) {
    return String.prototype.toLowerCase.apply(x, arguments.slice(1));
});
// --> TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

javascript

26
推荐指数
3
解决办法
3692
查看次数

标签 统计

javascript ×1