ES2018 学习笔记(3)标识符

以下内容来至 es2017 语言规范和 javascript 高级程序设计(第三版)

起源:标识符的定义

在红宝书 3.1.2 章节中,对标识符做了如下定义:

An identifier is the name of a variable, function, property, or function argument.

对属性名是不是标识符我存有疑问?再看看 ES 规范的定义:

Identifier:
IdentifierName but not ReservedWord

属性相关定义

属性

property

part of an object that associates a key (either a String value or a Symbol value) and a value

属性名(上面提到的 key)其实是 String 或 Symbol 类型值。

属性名

PropertyName [Yield, Await]:
LiteralPropertyName
ComputedPropertyName[?Yield, ?Await]

属性访问器

Property Accessors:
Properties are accessed by name, using either the dot notation:

MemberExpression . IdentifierName
CallExpression . IdentifierName
or the bracket notation:

MemberExpression [ Expression ]
CallExpression [ Expression ]
The dot notation is explained by the following syntactic conversion:

MemberExpression . IdentifierName
is identical in its behaviour to

MemberExpression [ ]
and similarly

CallExpression . IdentifierName
is identical in its behaviour to

CallExpression [ ]
where is the result of evaluating StringValue of IdentifierName.

属性两种表示法:

  • 点表示法,例如:Object.create
  • 方括号表示法,例如:Object['any string']Object['Expression' + 'Expression']

其中,点表示法中要求 . 之后必须跟着 IdentifierName。回看上面提到的标识符的定义:

IdentifierName but not ReservedWord

任何点表示法,都能转换成方括号表示法;反之,则行不通。

总结

属性名不是标识符。红宝书的定义比较通俗,但是总结的不算准确。

你可能感兴趣的:(javascript)