Internet Explorer 9是否会在数组和对象文字的末尾阻塞额外的逗号? [英] Does Internet Explorer 9 choke on extra commas at the end of array and object literals?

查看:115
本文介绍了Internet Explorer 9是否会在数组和对象文字的末尾阻塞额外的逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现代浏览器和Node.js等环境允许您说{a:1,b:2,}或[1,2,3,]。这在历史上一直是Internet Explorer的问题。这是在Internet Explorer 9中修复的吗?

Modern browsers and environments like Node.js allow you to say {a:1, b:2,} or [1,2,3,]. This has historically been problematic with Internet Explorer. Is this fixed in Internet Explorer 9?

推荐答案

这有两个不同的答案,一个用于对象初始化器中的悬空逗号和一个对于数组初始值设定项中的悬空逗号:

There are two different answers to this, one for dangling commas in object initializers and one for dangling commas in array initializers:

对于对象初始值设定项,例如:

var obj = {
    a: 1,
    b: 2,
    c: 3,
};

它固定在 IE8 及以上。在此测试: http://jsbin.com/UXuHopeC/1 )。 IE7和更早版本将在悬空逗号后在} 上抛出语法错误。

It's fixed in IE8 and above. Test it here: http://jsbin.com/UXuHopeC/1 (source). IE7 and earlier will throw a syntax error on the } after the dangling comma.

对于数组初始值设定项,例如:

var arr = [
    1,
    2,
    3,
];

IE9 及以上版本已修复。在此测试: http://jsbin.com/UXuHopeC/2 )。 IE8及更早版本将为该阵列提供四个条目,最后一个条目的值为 undefined 。 IE9及以上版本给它三个条目。

It was "fixed" in IE9 and above. Test it here: http://jsbin.com/UXuHopeC/2 (source). IE8 and earlier will give that array four entries, the last one having the value undefined. IE9 and above give it three entries.

我把固定放在引号中因为规范最初不清楚数组是否应该有一个最终的是否未定义是否输入,因此两种行为都不正确。只是IE走了一条路而其他人都去了另一条路。 : - )

I put "fixed" in quotes because the spec was originally unclear about whether the array should have a final undefined entry or not, so neither behavior was incorrect. It's just that IE went one way and everyone else went the other. :-)

这篇关于Internet Explorer 9是否会在数组和对象文字的末尾阻塞额外的逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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