"," at the end of a single element in a object in javascript is illegal? - Stack Overflow

I wonder why putting a comma at the end of a single element is legal in a JavaScript array:var array =

I wonder why putting a comma at the end of a single element is legal in a JavaScript array:

var array = [
    'foo', // no error in IDE
]

while putting it at the end of a single element in an object is illegal (at least my IDE - WebStorm - is flagging about an error):

var object = {
    'foo': 'bar', // error in IDE
}

Is this really illegal in JavaScript?

I wonder why putting a comma at the end of a single element is legal in a JavaScript array:

var array = [
    'foo', // no error in IDE
]

while putting it at the end of a single element in an object is illegal (at least my IDE - WebStorm - is flagging about an error):

var object = {
    'foo': 'bar', // error in IDE
}

Is this really illegal in JavaScript?

Share Improve this question edited Oct 20, 2017 at 20:08 Evan Wieland 1,5181 gold badge22 silver badges36 bronze badges asked Nov 7, 2010 at 2:35 ajsieajsie 79.7k110 gold badges284 silver badges386 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 16

In the ECMAScript 5 specification it is legal:

ObjectLiteral : 
    { } 
    { PropertyNameAndValueList } 
    { PropertyNameAndValueList , } 

It was illegal in ECMAScript 3.

ObjectLiteral : 
    { } 
    { PropertyNameAndValueList } 

I believe it was made legal to make things like this doable.

function getItems() {
    return {
        one : 1,
        two : 2,
        three : 3,
        //four : 4,
    };
}

Instead of this:

function getItems() {
    return {
        one : 1,
        two : 2,
        three : 3//,
        //four : 4,
    };
}

Saving some of the programmers time.

In addition to what ChaosPandion mentioned, there's an important difference between a , at the end of an object and an array. Empty commas in an array (technically called elisions) insert undefined elements at the corresponding position; this increases the length of the array but the values are undefined.

Edit: Thanks to ChaosPandion and CMS for pointing out the error. I just reread the specs and indeed a single trailing comma does not increase the length, however any additional trailing commas or any commas in the middle of an array will increase the length.

For example, [ 1,2,, ] is an array of length 3 and the same as [ 1, 2, undefined ]. Similarly, [ 1,,,2 ] is an array of length 4 and the same as [ 1, undefined, undefined, 2 ].

Strangely enough, when it comes to this "feature" of JavaScript, IE behaves correctly with a trailing comma in an array (it counts the extra element) while Firefox ignores the last undefined element.

Firefox tolerates it, most other sane browsers do so to. Do I still need to mention IE chokes on it?

Anyway, simply ensure there are no commas at the end of arrays/objects. While languages like PHP and - depending on the implementation - JavaScript are fine with it, it is extremely ugly - you also wouldn't leave a string at the end of a file unclosed just because the parser could consider EOF as a valid string end.

Yes, if you are using IE.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1739462986a4128395.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信