我已经声明了一个数组:
var employeeBanks =
[
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-02-01', validToDate: '2014-01-31' },
];
当尝试然后迭代数组的每个成员时:
for (var a = 0; a < employeeBanks.length; a++)
{
...
}
我收到一个错误,因为我的数组中有一个额外的项目:
employeeBanks
[0] = [object]
[1] = [object]
[2] = [object]
[3] = [object]
[prototype] = [object]
数组中的第 5 个元素无法识别。所以我得到一个错误。在 IE9、IE10 或边缘模式下运行 IE11,prototype
成员不出现(或在迭代数组时不出现)。
仅迭代数组成员的推荐技术是什么?未经我的许可,是否有任何其他预期的东西可能出现在数组中?
奖励截图:
完整来源:
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<script type="text/javascript">
var employeeBanks =
[
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-01-01', validToDate: '2013-12-31' },
{ validFromDate: '2013-02-01', validToDate: '2014-01-31' },
];
</script>
<body>
<p id="lblFoo">
<script type="text/javascript">
for (var a = 0; a < employeeBanks.length; a++)
{
document.getElementById("lblFoo").innerText = "Test failed";
document.getElementById("lblFoo").innerText = employeeBanks[a].validToDate;
}
document.getElementById("lblFoo").innerText = "Test complete";
</script>
</body>
</html>
删除<meta http-equiv="X-UA-Compatible" content="IE=8" />
,它的工作原理。