当然,这会返回您的期望:
["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 ×1