Javascript 对象属性引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9192517/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:53:08  来源:igfitidea点击:

Javascript object property quotes

javascriptobject

提问by machineaddict

Let's say I have the following object:

假设我有以下对象:

var VariableName = {
    firstProperty: 1,
    secondProperty: 2
}

Do I have to wrap the object properties in quotes like this?

我是否必须将对象属性用这样的引号括起来?

var VariableName = {
    'firstProperty': 1,
    'secondProperty': 2
}

Is this Single quotes in JavaScript object literalthe correct answer?

JavaScript 对象文字中的这个单引号是正确的答案吗?

回答by jAndy

No, you don't need to do that.

不,你不需要这样做。

The only reasons to quote object-keys are

引用对象键的唯一原因是

  • the property name is reserved/used by the browser/js engine (eg. "class" in IE)
  • you have special characters or white spaces in your key
  • 属性名称由浏览器/js 引擎保留/使用(例如 IE 中的“类”)
  • 您的密钥中有特殊字符或空格

so for instance

所以例如

var VariableName = {
    "some-prop": 42,  // needs quotation because of `-`
    "class": 'foobar' // doesn't syntatically require quotes, but it will fail on some IEs
    valid: 'yay' // no quotes required
};

回答by Sirko

You only have to use the quotes around the property, if the property name is a reserved word (like for, in, function, ...). That way you prevent Javascript from trying to interpret the keyword as a part of the language and most probably get a syntax error. Furthermore, if you want to use spaces in property names, you also have to use quotes. If your property names are just normal names without any collusion potential or spaces, you may use the syntax you prefer.

如果属性名称是保留字(如 for、in、function 等),则只需在属性周围使用引号。这样您就可以防止 Javascript 尝试将关键字解释为语言的一部分,并且很可能会出现语法错误。此外,如果要在属性名称中使用空格,还必须使用引号。如果您的属性名称只是没有任何共谋潜力或空格的普通名称,您可以使用您喜欢的语法。

One other possibility that requires quotes is the use of Javascript minifiers like google closure compiler, as it tends to replace all property names. If you put your property names in quotes, however, the closure compiler preserves the property as you coded it. This has some relevance when exporting objects in a library or using an parameter object.

另一种需要引号的可能性是使用 Javascript 缩小器,如 google 闭包编译器,因为它倾向于替换所有属性名称。但是,如果您将属性名称放在引号中,则闭包编译器会在您对其进行编码时保留该属性。这在导出库中的对象或使用参数对象时具有一定的相关性。

回答by Quentin

Property names in object literals must be strings, numbers or identifiers. If the name is a valid identifier, then you don't need quotes, otherwise they follow the same rules as strings.

对象字面量中的属性名称必须是字符串、数字或标识符。如果名称是有效标识符,则不需要引号,否则它们遵循与字符串相同的规则。

firstPropertyand secondPropertyare both valid identifiers, so you don't need quotes.

firstPropertysecondProperty都是有效的标识符,所以你不需要引号。

See page 65 of the specificationfor more details.

有关更多详细信息,请参阅规范的第 65 页。

回答by Matthew Gilliard

For Javascript you usually do not have to use quotes. You may use 'or "if you like, and you must use quotes if there is a clash between the name of your property and a JS reserved word like null. The answer you linked to seems to be correct, yes.

对于 Javascript,您通常不必使用引号。您可以使用'或者"如果您喜欢,并且如果您的属性名称和 JS 保留字(如null. 您链接的答案似乎是正确的,是的。

For JSON, you should use "around strings(including object property names)

对于 JSON,您应该使用"环绕字符串(包括对象属性名称)