gpt4 book ai didi

javascript - Firefox 中 `map` 的奇怪 javascript 行为

转载 作者:行者123 更新时间:2023-11-29 16:20:38 25 4
gpt4 key购买 nike

这是 Firefox 中 map 函数的一些奇怪的 javascript 行为。

在网络应用程序出现错误的情况下(当 firebug 因错误而暂停时)在 Firebug 控制台中键入以下内容:

["a", "b", "c", "d"].map(function(val, i, arr) {
return val + " " + i + " " + typeof arr;
});

产生以下意外结果:

["a undefined undefined",
"b undefined undefined",
"c undefined undefined",
"d undefined undefined"]

那时,如果我打开另一个空白选项卡并在空白选项卡的 Firebug 控制台中键入相同的语句,它会产生以下预期结果:

["a 0 object", 
"b 1 object",
"c 2 object",
"d 3 object"]

这意味着,在错误情况下,map 使用 1 个参数而不是预期的 3 个参数调用回调。

引自 MDN:( https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map )

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

是否由于应用强制 firefox 进入某种模式而导致意外行为?

(我使用的是 Firefox 12.0)

最佳答案

听起来您的应用程序中的某些其他代码可能正在覆盖 Array.prototype.map 函数——也许是未完全实现的 polyfill?如果您在控制台中键入 [].map,如果它没有显示 function map() { [native code] } 那么几乎可以肯定是这种情况。

关于javascript - Firefox 中 `map` 的奇怪 javascript 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10765498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com