一些JavaScript保留字用作变量 [英] Some JavaScript reserved words function as variables

查看:126
本文介绍了一些JavaScript保留字用作变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Crockford的 JavaScript:好的部分包含以下文字

Crockford's JavaScript: The Good Parts contains the following text.


保留字

以下单词保留在JavaScript中:

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中,以下代码将成功添加由保留字标识的两个数字。

Some of the reserved words appear to not be reserved in my installed interpreters. For example, in both Chrome 48 (beta) and node.js 0.10.40 the following code will successfully add two numbers identified by reserved words.

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

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

Why can I use these two reserved words as variable names? Am I missing something crucial?

推荐答案

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

abstract native (保留为此处更多内容)未来的关键字按较旧的ECMAScript规范(ECMAScript 1至3)。

and abstract and native (more here) were reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).

始终保留: enum

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

reserved when they are found in strict mode code:

implements package  protected  static  let  interface  private  public

保留在模块代码中时保留:等待

reserved when they are found in module code: await

这篇关于一些JavaScript保留字用作变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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