IE9 JavaScript数组初始化bug [英] IE9 JavaScript array initialization bug

查看:73
本文介绍了IE9 JavaScript数组初始化bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,IE9中的JS实现在处理数组文字时包含(IMO,严重)错误。

Apparently JS implementation in IE9 contains (IMO, critical) bug in handling array literals.

在IE9中,在某些情况下此代码:

In IE9 in some cases this code:

var a = [1,2,3,4,];

将创建长度为5的数组,最后一个元素等于 undefined

will create array of length 5 with last element equals to undefined.

以下是我的KiTE引擎测试页面的两个版本:

Here are two versions of my KiTE engine test pages:

  • http://terrainformatica.com/kite/test-kite.htm - works in IE9
  • http://terrainformatica.com/kite/test-kite-ie9-bug.htm - fails in IE9

唯一的区别是第一个文档包含初始化为<的data.contacts属性code> [1,2,3,4] ,第二个为 [1,2,3,4,]

The only difference is that first document contains data.contacts property initialized as [1,2,3,4] and second one as [1,2,3,4,].

内部IE调试器报告 data.contacts 数组在第二种情况下包含5个元素。没有调试器,此代码在kite.js中的第98行失败(尝试获取 undefined 的属性 - 该data.content数组的第五个元素)

Internal IE debugger reports that data.contacts array contains 5 elements in second case. Without debugger this code fails at line 98 in kite.js (trying to get property of undefined - fifth element of that data.content array )

问题:


  1. 人们通常在IE中报告错误的方式和地点?

  2. 你见过类似这个问题的东西吗?我正在寻找这个问题可重现的最简单的情况。

更新:这是测试 http:// jsfiddle.net/hmAms/ 其中所有浏览器(包括IE9)都同意 var a = [1,2,3,4,]; 的事实是长度为4.

Update: here is the test http://jsfiddle.net/hmAms/ where all browsers (IE9 included) agree on the fact that var a = [1,2,3,4,]; is of length 4.

推荐答案

应忽略数组文字中的单个尾随逗号。两个尾随逗号是一个省略号,应该在数组的长度上添加一个逗号。所以:

A single trailing comma in an array literal should be ignored. Two trailing commas is an elision and should add one to the array's length. So:

alert( [1,2,3,4,].length );   // 4

alert( [1,2,3,4,,].length );  // 5

某些版本的IE(< 9?)将单个训练逗号视为elison并且错误地将一个加到长度上,因此上面的结果是5和6的结果。这与ECMA-262§11.1.3不一致,因此是一个错误。

Some versions of IE (< 9?) treat the single trainling comma as an elison and incorrectly add one to length, so the results above are 5 and 6 respsectively. That is inconsistent with ECMA-262 §11.1.3 and therefore is a bug.

省略的目的是增加数组长度而不创建额外的属性或直接分配长度,所以:

The purpose of an elision is to increase array length without creating a extra property or assigning directly to length, so:

var x = [,1,,];

相当于:

var x = new Array(3);
x[1] = 1;

两种情况下的结果都应该是长度为3的数组和一个名为1的属性值1.前导逗号和尾随逗号对是精简,它们影响长度,它们不创建属性。 IE正确解释了前导逗号但错误地将两个尾随逗号都解释为elisions,将长度增加了太多。

The result in both cases should be an array with length 3 and one property named '1' with value 1. The leading comma and trailing comma pair are elisions, they only affect the length, they do not create properties. IE interprets the leading comma correctly but incorrectly interprets both trailing commas as elisions, incrementing the length by 1 too many.

var x = [,1,,3,,];
var s = 'length: ' + x.length;

for (var p in x) {
  s += '\nindex ' + p + ' has value ' +  x[p]; 
}
alert(s);

结果应为:

length: 5
index 1 has value 1
index 3 has value 3

顺便说一句,这个bug可能已经存在,因为IE允许数组文字,版本4至少(1997?)。

Incidentally, this bug has probably been around since IE allowed array literals, version 4 at least (1997?).

这篇关于IE9 JavaScript数组初始化bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆