我有下面的JS。
IE8中的错误是:
' S'是null或不是对象
任何想法都会受到赞赏吗?
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
];
var obj = t[0];
for (var prop in obj) {
console.log(prop+": "+obj[prop]);
}
for (var i = t.length - 1; i >= 0; i--) {
var l = t[i];
var s = l.s;
console.log(s);
}
答案 0 :(得分:2)
你的t数组中的最后一个对象后面有一个逗号。删除它,因为它是一个未定义的循环中的第一个对象,因为在IE8中它最初不会抛出错误但它会获得一个空对象。这就是你得到错误的原因。
您的代码:
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
undefined // How IE8 parses it and your loop starts here
];
这不会抛出错误:
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"} // No comma so array is terminated here
];
答案 1 :(得分:2)
数组中的最后一个元素为null。尝试删除数组中的最后一个“,”
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"}
];
答案 2 :(得分:1)
常见错误:
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"}, // remove the comma
];
var t = [
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"},
{s: "blah", e: "blah blah"}
];
现在在IE8中运行正常......我测试了它:http://jsfiddle.net/SnakeEyes/qFgjh/