为什么有些对象文字属性被引用而另一些则没有?
我一直看到这样的情况:声明的对象文字中某些键用引号引起来,而另一些则没有。 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"
};
用引号包裹前两个属性键(for
和 class
)有何意义,同时让其他人少引用?有什么区别吗?
我一直在研究 ECMAScript 5 规范;我所能找到的只是[第 15.12.3 节的注释 6,强调我的]:
注 6 对象被渲染为 左大括号后跟零或 更多属性,分开 逗号,用右大括号括起来。 A 属性是带引号的字符串 代表键或属性名称, 冒号,然后是字符串化的 财产价值。渲染了一个数组 作为左括号,后跟 零个或多个值,用 逗号,用右括号括起来。
不过,这仅指 JSON 的字符串化。
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.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?
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]:
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.
However, this refers only to the stringification of JSON.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些是 Javascript 保留字,并且(虽然不是真正必要)语言的语法要求将它们括起来。
严格来说,纯“JSON”表示法要求所有的“key”字符串被引用。然而,Javascript 本身可以接受未加引号的有效标识符(但不是保留字)的键。
Those are Javascript reserved words, and (though not really necessary) the syntax of the language requires that they be quoted.
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.
此时(两年多后)有理由引用对象文字属性。如果想要使用闭包编译器缩小代码,他们可能需要使其他源文件可以访问这些属性。在这种情况下,他们将希望避免编译器重命名符号。通过引用属性名称,闭包编译器不会缩小(重命名)它们。
请参阅:删除您想要保留的代码
(这至少适用于ADVANCED_OPTIMIZATIONS 设置。)
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.)
Javascript 语言关键字或保留关键字总是用引号括起来。
Javascript language keywords or reserved keywords are always surrounded by quotes in there.
for
和class
是语言关键字。当这些不被引用时,你的解释器会抛出一个 SyntaxError 。请参阅您链接到的规范中的第 7.6.1.1 节。
for
andclass
are language keywords. Your interpreter would throw a SyntaxError when those are unquoted.See section 7.6.1.1 in the Spec you linked to.
Javascript 有很多保留字,这些保留字实际上并未被该语言使用,我认为这些保留字是为将来可能使用而保留的。尽管 Javascript 实际上并不使用类,但
class
就是其中之一。另一个是goto
,而且绝对没有机会使用它。然而,结果是,如果您想将它们用作 json 键,则必须将其加引号。严格来说,您可能应该始终引用您的密钥,以避免陷入 javascript 未使用保留字陷阱的可能性(请注意 - 我从来没有这样做)。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 isgoto
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).