This question already has an answer here:
这个问题已经有了答案:
- What is the difference between object keys with quotes and without quotes? 6 answers
- 带引号和不带引号的对象键有什么区别?6答案
I see this all the time: object literals declared such that some keys are surrounded with quotes and others are not. An example from jQuery 1.4.2:
我经常看到这样的情况:对象文本声明这样一些键被引号包围,而其他的则不是。jQuery 1.4.2的一个例子:
jQuery.props = {
"for": "htmlFor",
"class": "className",
readonly: "readOnly",
maxlength: "maxLength",
cellspacing: "cellSpacing",
rowspan: "rowSpan",
colspan: "colSpan",
tabindex: "tabIndex",
usemap: "useMap",
frameborder: "frameBorder"
};
What is the significance of wrapping the first two property keys (for
and class
) with quotes, while leaving the others quote-less? Are there any differences at all?
使用引号将前两个属性键(for和class)包装起来,而将其他属性键保留为无引用,这有什么意义呢?有什么不同吗?
I've been poking around the ECMAScript 5 specification; all I've been able to find is [Note 6 of Section 15.12.3, emphasis mine]:
我一直在研究ECMAScript 5规范;我所能找到的是[第15.12.3节注6,重点是我的]:
NOTE 6 An object is rendered as an opening left brace followed by zero or more properties, separated with commas, closed with a right brace. A property is a quoted String representing the key or property name, a colon, and then the stringified property value. An array is rendered as an opening left bracket followed by zero or more values, separated with commas, closed with a right bracket.
注释6一个对象被呈现为一个左括号,后面是零或更多的属性,用逗号分隔,用右括号结束。属性是一个引用的字符串,表示键或属性名、冒号,然后是字符串化的属性值。数组被呈现为左括号,后面跟着零或多个值,用逗号分隔,右括号结束。
However, this refers only to the stringification of JSON.
然而,这仅指JSON的绑定。
5 个解决方案
#1
37
Those are Javascript reserved words, and (though not really necessary) the syntax of the language requires that they be quoted.
这些是Javascript保留字,(虽然不是真正必要)语言的语法要求它们被引用。
Strictly speaking, pure "JSON" notation requires that all of the "key" strings be quoted. Javascript itself however is OK with keys that are valid identifiers (but not reserved words) being unquoted.
严格地说,纯“JSON”表示法要求引用所有“key”字符串。但是,对于没有引用的有效标识符(但不是保留词)的键,Javascript本身是可以的。
#2
5
There is a reason at this point (two plus years later) to quote object literal properties. If one wants to minify their code using the Closure Compiler they may need to make the properties accessible to other source files. In that case, they will want to avoid having symbols renamed by the compiler. By quoting the property name, the Closure Compiler will not minify (rename) them.
在这一点(两年多之后)引用对象的文字属性是有原因的。如果希望使用闭包编译器缩小代码,则可能需要使其他源文件可以访问这些属性。在这种情况下,他们希望避免由编译器重新命名符号。通过引用属性名,闭包编译器不会缩小(重命名)它们。
See: Removal of code you want to keep
查看:删除您想保留的代码。
(This applies to at least the ADVANCED_OPTIMIZATIONS setting.)
(这至少适用于advanced_optimization sets)
#3
2
Javascript language keywords or reserved keywords are always surrounded by quotes in there.
Javascript语言关键字或保留关键字总是被引号包围。
#4
1
for
and class
are language keywords. Your interpreter would throw a SyntaxError when those are unquoted.
for和class是语言关键字。当没有引用时,解释器会抛出一个语法错误。
See section 7.6.1.1 in the Spec you linked to.
请参阅您所链接的规范中的7.6.1.1节。
#5
1
Javascript has a lot of reserved words that are not actually used by the language which I think were reserved for possible future use. class
is one of these even though Javascript does not actually use classes. Another is goto
and there's absolutely no chance of that ever being used. The result, however, is that if you want to use these as a json key then it has to be quoted. Strictly speaking you should probably always quote your keys just to avoid the possibility of falling foul of the javascript unused reserved word trap (mind you - I never do).
Javascript有很多保留的词实际上并没有被语言使用,我认为它们是为将来可能的使用而保留的。类就是其中之一,尽管Javascript实际上并不使用类。另一种是goto,它绝对不可能被使用。然而,结果是,如果您想将它们用作json键,那么它必须被引用。严格地说,您应该总是引用您的键,以避免与javascript未使用的保留词陷阱发生冲突(请注意——我从不这样做)。
#1
37
Those are Javascript reserved words, and (though not really necessary) the syntax of the language requires that they be quoted.
这些是Javascript保留字,(虽然不是真正必要)语言的语法要求它们被引用。
Strictly speaking, pure "JSON" notation requires that all of the "key" strings be quoted. Javascript itself however is OK with keys that are valid identifiers (but not reserved words) being unquoted.
严格地说,纯“JSON”表示法要求引用所有“key”字符串。但是,对于没有引用的有效标识符(但不是保留词)的键,Javascript本身是可以的。
#2
5
There is a reason at this point (two plus years later) to quote object literal properties. If one wants to minify their code using the Closure Compiler they may need to make the properties accessible to other source files. In that case, they will want to avoid having symbols renamed by the compiler. By quoting the property name, the Closure Compiler will not minify (rename) them.
在这一点(两年多之后)引用对象的文字属性是有原因的。如果希望使用闭包编译器缩小代码,则可能需要使其他源文件可以访问这些属性。在这种情况下,他们希望避免由编译器重新命名符号。通过引用属性名,闭包编译器不会缩小(重命名)它们。
See: Removal of code you want to keep
查看:删除您想保留的代码。
(This applies to at least the ADVANCED_OPTIMIZATIONS setting.)
(这至少适用于advanced_optimization sets)
#3
2
Javascript language keywords or reserved keywords are always surrounded by quotes in there.
Javascript语言关键字或保留关键字总是被引号包围。
#4
1
for
and class
are language keywords. Your interpreter would throw a SyntaxError when those are unquoted.
for和class是语言关键字。当没有引用时,解释器会抛出一个语法错误。
See section 7.6.1.1 in the Spec you linked to.
请参阅您所链接的规范中的7.6.1.1节。
#5
1
Javascript has a lot of reserved words that are not actually used by the language which I think were reserved for possible future use. class
is one of these even though Javascript does not actually use classes. Another is goto
and there's absolutely no chance of that ever being used. The result, however, is that if you want to use these as a json key then it has to be quoted. Strictly speaking you should probably always quote your keys just to avoid the possibility of falling foul of the javascript unused reserved word trap (mind you - I never do).
Javascript有很多保留的词实际上并没有被语言使用,我认为它们是为将来可能的使用而保留的。类就是其中之一,尽管Javascript实际上并不使用类。另一种是goto,它绝对不可能被使用。然而,结果是,如果您想将它们用作json键,那么它必须被引用。严格地说,您应该总是引用您的键,以避免与javascript未使用的保留词陷阱发生冲突(请注意——我从不这样做)。