当设置为JavaScript对象的属性名称时,是否需要引用保留字? [英] Do reserved words need to be quoted when set as property names of JavaScript objects?

查看:157
本文介绍了当设置为JavaScript对象的属性名称时,是否需要引用保留字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个对象文字或 jQuery(html,attributes ) 对象,是否有任何规范说明保留字或未来的保留字必须引用?



或者,例如,可以将 class 设置为对象的属性名称,而不使用引号括起来财产名称,没有违反有关标识符,财产名称或保留字的使用的规范?



寻求关于这个问题的结论性答案以避免混淆。



  let objLit = {class:123,var:abc, let:456,const:def,import:789} console.dir(objLit); jQuery(< div> 012< / div>,{class:ghi})。appendTo(body) ;  

 < script src =https:// ajax .googleapis.com / AJAX /库/ jquery的/ 2.1.1 / jquery.min.js>< /脚本>  



相关:




  • < a href =https://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes>带引号的对象键之间有什么区别并且没有引号?


  • 对此回答<的评论




规格






标识符名称是根据语法解释的标记http://unicode.org/versions/Unicode4.0.0/ch05.pdfrel =noreferrer> Unicode
标准第5章的标识符部分
有一些小的修改

标识符是不是ReservedWord的标识符



解决方案

ECMAScript 5 +



不,自ECMAScript 5起,不需要报价。原因如下:



如您的帖子所述,来自ECMAScript®5.1语言规范


7.6 标识符名称和标识符



标识符名称是根据标识符部分中给出的语法解释的标记。 Unicode标准的第5章,有一些小的修改。 标识符是一个 IdentifierName ,它不是 ReservedWord (请参阅 7.6.1 )。



[...]



语法

  Identifier :: 
IdentifierName但不是ReservedWord




根据规范, ReservedWord 是:


7.6.1 保留字



保留字是 IdentifierName ,不能用作标识符



语法

  ReservedWord :: 
关键字
FutureReservedWord
NullLiteral
BooleanLiteral


这包括关键字,将来的关键字, null 和布尔文字。完整列表如下:


7.6.1.1 关键字



  break do instanceof typeof 
case else new var
catch finally return void
继续进行切换,而
调试器函数使用
默认如果在
中抛出
$ b



7.6.1.2 未来保留字



  class enum扩展超
const导出导入



7.8.1 空文字



  null 



7.8.2 布尔文字



  true 
false


以上(第7.6节)暗示 IdentifierName s可以是 ReservedWord s,并且来自对象初始值设定项


11.1.5 对象初始化程序



[...]



语法

  ObjectLiteral:
{}
{PropertyNameAndValueList}
{PropertyNameAndValueList,}


其中 PropertyName 按规格说明:


  PropertyName:
IdentifierName
StringLiteral
Num ericLiteral


如您所见, PropertyName 可能是 IdentifierName ,因此允许 ReservedWord s 的PropertyName 秒。这最终告诉我们, by specification ,允许 ReservedWord s,例如 class var as PropertyName s不加引号,就像字符串文字或数字文字一样。






ECMAScript< 5



为了更深入地了解为什么在ES5之前的版本中不允许这样做,你必须看看如何定义 PropertyName 。根据ECMAScript®3语言规范


  PropertyName:
Identifier
StringLiteral
NumericLiteral


尽可能看, PropertyName 是一个 Identifer - 而不是 IdentifierName ,因此导致 ReservedWord s无法作为 PropertyName s。


Given an object literal, or jQuery(html, attributes) object, does any specification state that reserved words, or future reserved words MUST be quoted?

Or, can, for example, class be set as a property name of an object without using quotes to surround the property name, without the practice being contrary to a specification concerning identifiers, property names, or use of reserved words?

Seeking a conclusive answer as to this question to avoid confusion.

let objLit = {
  class: 123,
  var: "abc",
  let: 456,
  const: "def",
  import: 789
}

console.dir(objLit);

jQuery("<div>012</div>", {
  class: "ghi"
})
.appendTo("body");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

Related:

Specification

Identifier Names are tokens that are interpreted according to the grammar given in the "Identifiers" section of chapter 5 of the Unicode standard, with some small modifications.
An Identifier is an IdentifierName that is not a ReservedWord

解决方案

ECMAScript 5+

No, quotes were not needed since ECMAScript 5. Here's why:

As mentioned in your post, from the ECMAScript® 5.1 Language Specification:

7.6 Identifier Names and Identifiers

Identifier Names are tokens that are interpreted according to the grammar given in the "Identifiers" section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1).

[...]

Syntax

Identifier ::
  IdentifierName but not ReservedWord

By specification, a ReservedWord is:

7.6.1 Reserved Words

A reserved word is an IdentifierName that cannot be used as an Identifier.

Syntax

ReservedWord :: 
  Keyword
  FutureReservedWord
  NullLiteral
  BooleanLiteral

This includes keywords, future keywords, null, and boolean literals. The full list is as follows:

7.6.1.1 Keywords

break    do       instanceof typeof
case     else     new        var
catch    finally  return     void
continue for      switch     while
debugger function this       with
default  if       throw 
delete   in       try   

7.6.1.2 Future Reserved Words

class enum   extends super
const export import

7.8.1 Null Literals

null

7.8.2 Boolean Literals

true
false

The above (Section 7.6) implies that IdentifierNames can be ReservedWords, and from the specification for object initializers:

11.1.5 Object Initialiser

[...]

Syntax

ObjectLiteral :
  { }
  { PropertyNameAndValueList }
  { PropertyNameAndValueList , }

Where PropertyName is, by specification:

PropertyName :
  IdentifierName
  StringLiteral
  NumericLiteral

As you can see, a PropertyName may be an IdentifierName, thus allowing ReservedWords to be PropertyNames. That conclusively tells us that, by specification, it is allowed to have ReservedWords such as class and var as PropertyNames unquoted just like string literals or numeric literals.


ECMAScript <5

To go more in depth as to why this wasn't allowed in previous versions before ES5, you have to look at how PropertyName was defined. Per the ECMAScript® 3 Language Specification:

PropertyName :
  Identifier
  StringLiteral
  NumericLiteral

As you can see, PropertyName was an Identifer - not an IdentifierName, thus leading to the inability for ReservedWords as PropertyNames.

这篇关于当设置为JavaScript对象的属性名称时,是否需要引用保留字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆