Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

javascript - 数组和对象中的尾部逗号是否属于规范的一部分?(Are trailing commas in arrays and objects part of the spec?)

Are trailing commas standard in JavaScript, or do most browsers like Chrome and Firefox just tolerate them?(JavaScript中的尾部逗号是标准格式,还是像Chrome和Firefox这样的大多数浏览器都可以接受?)

I thought they were standard, but IE8 puked after encountering one—of course IE not supporting something hardly means it's not standard.(我以为它们是标准的,但是IE8在遇到一个标准后就呕吐了—当然IE 8不支持某些功能也不意味着它不是标准的。)

Here's an example of what I mean (after the last element of the books array):(这是我的意思的示例(在books数组的最后一个元素之后):)

var viewModel = {
    books: ko.observableArray([
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } }, // <--right there
    ]),
    currentTemplate: ko.observable("bookTemplate1"),
    displayTemplate: function() { return viewModel.currentTemplate(); }
};
  ask by Adam Rackis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specs: ECMAScript 5 and ECMAScript 3(规格: ECMAScript 5ECMAScript 3)


Section 11.1.5 in the ECMAScript 5 specification:(ECMAScript 5规范中的11.1.5节 :)

ObjectLiteral :
    { }
    { PropertyNameAndValueList }
    { PropertyNameAndValueList , }

So yes, it is part of the specification.(是的,它是规范的一部分。)

Update: Apparently this is new in ES5.(更新:显然,这是ES5中的新功能。)

In ES3 (page 41), the definition was just:(在ES3(第41页)中,定义仅为:)
ObjectLiteral :
    { }
    { PropertyNameAndValueList }

For arrays literals ( Section 11.1.4 ) it is even more interesting ( Update: this already existed in ES3):(对于数组文字( 第11.1.4节 ),它甚至更有趣( 更新: ES3中已经存在):)

ArrayLiteral :
    [ Elisionopt ]
    [ ElementList ]
    [ ElementList , Elision_opt ]

(where Elision_opt is Elision opt , meaning the Elision is optional)((其中Elision_opt是Elision opt ,这意味着Elision是可选的))

Elision is defined as(Elision定义为)

Elision :
    ,
    Elision ,

So, an array literal like(因此,像这样的数组文字)

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

is perfectly legal.(是完全合法的。)

This creates an array with two elements but sets the array length to 2 + 3 = 5 .(这将创建一个包含两个元素的数组,但是将数组长度设置为2 + 3 = 5 。)

Don't expect too much from IE (before IE9)...(不要对IE期望太多(在IE9之前)...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...