这似乎是从最新的Chrome更新开始的,我试图找到关于它的信息但未成功。请看下面的代码:Chrome向数组添加'remove'元素?
var lines = ['asdf','qwer',''];
for (var i in lines) console.log(i, lines[i]);
如果我运行在Firefox的“萤火虫”控制台,我的输出,符合市场预期,这就是:
0 asdf
1 qwer
2 (an empty string)
然而,当我在Chrome控制台运行,我得到这个:
0 asdf
1 qwer
2
remove function (from, to)
{
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
}
我注意到额外的“删除”项目定期提出。问题是,它打破几个jQuery插件,我下载使用,其中有这样的代码:
for (var i in lines) {
line = lines[i].split("=");
...
由于额外的元素是一个函数,而不是一个字符串,它不具备split()
函数,所以我的代码在错误的轨道上停下来。我不想通过手动从数组中删除元素来完成所有的代码,那么是否有一些标志可以设置,或者我可以在执行前运行,以防止添加额外的元素?
请*不要*用阵列中的'for ... in' –
chrome究竟是什么版本?看起来这更像是一个插件向数组原型添加'remove'的插件。 – numbers1311407
'(var in array)'包含继承的属性。你应该使用数字迭代或者检查'lines.hasOwnProperty(i)'。见http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript/3010848#3010848 – Barmar