Javascript 数组和对象中的尾随逗号是规范的一部分吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7246618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are trailing commas in arrays and objects part of the spec?
提问by Adam Rackis
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不支持某些东西并不意味着它不是标准的。
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(); }
};
采纳答案by Felix Kling
Specs: ECMAScript 5and ECMAScript 3
Section 11.1.5in 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. In ES3 (page 41), the definition was just:
更新:显然这是 ES5 中的新功能。在 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 Elisionopt, 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 之前)...
回答by Joey Sabey
Just a quick reminder/warning that this is one of the areas in which the JavaScript/ECMAScript standardand JSON standarddiffer; trailing commas are validin JS but not validin JSON.
只是快速提醒/警告,这是JavaScript/ECMAScript 标准和JSON 标准不同的领域之一;尾随逗号在 JS中有效,但在 JSON 中无效。
回答by seeg
What is even funnier, IE7 gives
更有趣的是,IE7 给出了
[1,].length --> 2
while Firefox and Chrome
而 Firefox 和 Chrome
[1,].length --> 1
回答by Endophage
You can find the specification for javascript (aka ECMA Script) here. You can find the relevant definition for arrays on page 63 and as Felix noted, the object definition a couple of pages later on page 65.
您可以在此处找到 javascript(又名 ECMA 脚本)的规范。您可以在第 63 页找到数组的相关定义,正如 Felix 所指出的,对象定义稍后在第 65 页找到。
While this specification says it is fine to have a trailing ,
I don't know if that would be true looking back a few versions. As you've noted IE8- will crap itself if you leave a trailing comma but Chrome and FF handle it fine.
虽然这个规范说有一个尾随很好,但,
我不知道回顾几个版本是否真的如此。正如您所指出的,IE8- 如果您留下一个尾随逗号,它会自行处理,但 Chrome 和 FF 处理得很好。
回答by garrettmac
Let's break this down.
让我们分解一下。
Are trailing commas standard in JavaScript?
JavaScript 中的尾随逗号是标准的吗?
Yes. As of the ECMAScript 5 specification (also part of the Google and Airbnb style guide)
是的。从 ECMAScript 5 规范开始(也是 Google 和 Airbnb 风格指南的一部分)
Do most browsers like Chrome and Firefox just tolerate them?
大多数浏览器(如 Chrome 和 Firefox)是否只是容忍它们?
This is an ECMAScript 5 support question.
这是 ECMAScript 5 支持问题。
Transpilers like Babel will remove the additional trailing comma in the transpiled code which means you don't have to worry about the trailing comma problem in legacy browsers.
像 Babel 这样的转译器会删除转译后的代码中额外的尾随逗号,这意味着您不必担心旧浏览器中的尾随逗号问题。
So that means:
所以这意味着:
var heroes = [
'Batman',
'Superman',
];
// heroes.length === 2 (not 3)
So chances are if you're using anything ES5 and up you don't need to worry about it.
因此,如果您使用的是 ES5 及更高版本的任何东西,则很有可能无需担心。
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不支持某些东西并不意味着它不是标准的。
Again, that's an ECMAScript 5 support question. IE8 doesn't support ECMAScript 5 (only IE9 and up)
同样,这是一个 ECMAScript 5 支持问题。IE8 不支持 ECMAScript 5(仅 IE9 及更高版本)
I would highly recommend taking a look Airbnb's ES5 deprecated Documentation https://github.com/airbnb/javascript/blob/es5-deprecated/es5/README.md#commas
我强烈建议您查看 Airbnb 的 ES5 弃用文档 https://github.com/airbnb/javascript/blob/es5-deprecated/es5/README.md#commas
I would also recommend the Mozilla's Docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas
我还会推荐 Mozilla 的文档:https:
//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas
回答by Nicolas Zozol
On Chrome 52 :
在 Chrome 52 上:
[1,].length --> 1
[1,2,].length --> 2
[].length --> 0
[,].length --> 1 <<<<=== OUHHHHH !!!!
I just don't like trailing commas. People usually use them in Open Source projects for avoiding to erase the line written by another commiter. See the same question in Python : https://stackoverflow.com/a/11597911/968988
我只是不喜欢尾随逗号。人们通常在开源项目中使用它们,以避免擦除其他提交者编写的行。在 Python 中看到同样的问题:https: //stackoverflow.com/a/11597911/968988