Javascript 带引号和不带引号的对象键有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4348478/
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
What is the difference between object keys with quotes and without quotes?
提问by meo
Is there any difference between
有什么区别吗
obj = {'foo': 'bar'}
and
和
obj = {foo: 'bar'}
I have noticed that you can't use -
in the key when I don't use the quotes. But does it actually make a difference? If yes, which?
我注意到-
当我不使用引号时,您不能在键中使用。但这真的有区别吗?如果是,是哪个?
采纳答案by bdukes
No, the quotes do not make a difference (unless, as you noted, you want to use a key that's not a valid JavaScript identifier).
不,引号没有区别(除非,正如您所指出的,您想使用不是有效 JavaScript 标识符的键)。
As a side note, the JSON data exchange formatdoesrequire double quotes around identifiers (and does notallow single quotes).
作为一个侧面说明,JSON数据交换格式确实需要标识周围的双引号(和它不会让单引号)。
回答by Mathias Bynens
From Unquoted property names / object keys in JavaScript, my write-up on the subject:
来自JavaScript 中的 Unquoted property names / object keys,我关于这个主题的文章:
Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.
[…]
Bracket notation can safely be used for all property names.
[…]
Dot notation can onlybe used when the property name is a valid identifier name.
如果属性名称是数字文字或有效的标识符名称,则只能省略引号。
[…]
括号表示法可以安全地用于所有属性名称。
[…]
点符号只能在属性名称是有效标识符名称时使用。
Note that reserved words are allowed to be used as unquoted property names in ES5. However, for backwards compatibility with ES3, I'd suggest quoting them anyway.
请注意,ES5 中允许将保留字用作不带引号的属性名称。但是,为了与 ES3 向后兼容,我建议无论如何都要引用它们。
I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.
我还制作了一个工具,它会告诉您是否可以在不使用引号和/或点符号的情况下使用任何给定的属性名称。在mothereff.in/js-properties尝试一下。
回答by Raynos
There is no difference here. Just a matter of style. One of the reasons for doing this is being able to use 'super' or 'class' as a key since those are reserved keywords.
这里没有区别。只是风格问题。这样做的原因之一是能够使用 'super' 或 'class' 作为键,因为它们是保留关键字。
Some people might be tempted to pass in a string with whitespace then call o['I can have whitespace'] But I would call that bad practice.
有些人可能会试图传入一个带有空格的字符串,然后调用 o['I can have whitespace'] 但我认为这是不好的做法。
回答by 01001111
No, not to javascript. However, some JSON parsers will fail when the quotes around the keys are not present.
不,不是 javascript。但是,当键周围的引号不存在时,某些 JSON 解析器将失败。
回答by spekary
There are some situations where they are different. For example, if you are using jQuery, and you are making a list of parameters to pass when calling the jQuery $() command to create an element, quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object. See jQuery(), near the bottom:
在某些情况下,它们是不同的。例如,如果您正在使用 jQuery,并且在调用 jQuery $() 命令创建元素时正在制作要传递的参数列表,则引用的词将转换为参数,而未引用的词则转换为函数。例如,"size" 将设置对象的大小属性,而 size(无引号)将调用对象上的 size() 函数。请参阅底部附近的jQuery():
While the second argument is convenient, its flexibility can lead to unintended consequences (e.g. $( "
<input>
", {size: "4"} ) calling the .size() method instead of setting the size attribute). The previous code block could thus be written instead as:
虽然第二个参数很方便,但它的灵活性可能会导致意想不到的后果(例如 $( "
<input>
", {size: "4"} )调用 .size() 方法而不是设置 size 属性)。因此,前面的代码块可以写成: