一些 JavaScript 保留字作为变量

Some JavaScript reserved words function as variables

Crockford 的 JavaScript:好的部分 包含 the following text

Reserved Words

The following words are reserved in JavaScript:

abstract boolean break byte case catch char class const continue
 debugger default delete do double else enum export extends false final
 finally float for
 function goto if implements import in instanceof int interface long native new null
 package private protected public return short static super switch synchronized this
 throw throws transient true try typeof var volatile void while with

Most of these words are not used in the language.

They cannot be used to name variables or parameters. When reserved words are used as keys in object literals, they must be quoted. They cannot be used with the dot notation, so it is sometimes necessary to use the bracket notation instead:

var method;                // ok
var class;                 // illegal
object = {box: value};     // ok
object = {case: value};    // illegal
object = {'case': value};  // ok
object.box = value;        // ok
object.case = value;       // illegal
object['case'] = value;    // ok

有些保留字在我安装的解释器中似乎没有保留。例如,在 Chrome 48(测试版)和 node.js 0.10.40 中,以下代码将成功添加两个由保留字标识的数字。

var abstract = 1;
var native = 1;
abstract + native;
> 2

为什么我可以使用这两个保留字作为变量名?我错过了一些重要的东西吗?

Reserved keywords as of ECMAScript 6

break case class catch const continue debugger default delete do else
export extends finally for function if import in instanceof new return
super switch this throw try typeof var void while with yield

and abstract and native (more here) 被旧的 ECMAScript 规范(ECMAScript 1 到 3)保留为未来的关键字。

始终保留:enum

在严格模式代码中找到时保留:

implements package  protected  static  let  interface  private  public

在模块代码中找到时保留:await

保留字(也称为保留标识符或关键字)是不能用作标识符的字,例如变量、函数或标签的名称——它是"reserved from use"。保留字或关键字在编程语言中具有特殊含义。它们用于识别支持系统的语言中的数据类型,识别块和循环等,因此它们的功能已经在系统库中定义。

在您的代码中包含关键字或保留字,在您 运行 您的代码时会给其他开发人员和编译器造成混淆。这就是为什么许多编程语言不允许使用保留字的原因。还有其他一些具有类似关键字的编程语言;例如 C、C++、C# 和 Java 它们具有共同点。

在这里您可以获得Reserved Words in JavaScript的最新列表,其中还包含有用的示例。