這似乎是從最新的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