JavaScript 中的保留关键字

发布于 2024-07-04 12:39:52 字数 37 浏览 15 评论 0原文

保留哪些 JavaScript 关键字(函数名称、变量等)?

What JavaScript keywords (function names, variables, etc) are reserved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

べ映画 2024-07-11 12:40:02

benc 的回答非常好,但是为了我的两分钱,我喜欢 w3schools 的页面:

http:// /www.w3schools.com/js/js_reserved.asp

除了列出标准保留的关键字之外,它还列出了一长串您在某些情况下应该避免的关键字; 例如,在编写要在浏览器中运行的代码时不使用名称 alert。 它帮助我弄清楚为什么某些单词在我的编辑器中突出显示为关键字,即使我知道它们不是关键字。

benc's answer is excellent, but for my two cents, I like the w3schools' page on this:

http://www.w3schools.com/js/js_reserved.asp

In addition to listing the keywords reserved by the standard, it also has a long list of keywords you should avoid in certain contexts; for example, not using the name alert when writing code to be run in a browser. It helped me figure out why certain words were highlighting as keywords in my editor even though I knew they weren't keywords.

新人笑 2024-07-11 12:40:01

以下是 Eloquent JavaScript 书中的列表:

  • break
  • case
  • catch
  • class
  • const
  • <代码>继续
  • 调试器
  • 默认
  • 删除
  • 执行
  • 其他
  • 枚举
  • 导出
  • 扩展
  • false
  • 最后
  • for
  • 函数< /code>
  • if
  • 实现
  • 导入
  • in
  • instanceof
  • 接口
  • let
  • new
  • null
  • package
  • 私有
  • 受保护
  • <代码>公共
  • > <代码>返回</
  • 代码> <代码>静态</
  • 代码> <代码>超级
  • > <代码>切换 <代码
  • >此 <代码
  • >抛出
  • true
  • try
  • typeof
  • var
  • void
  • while< /code>
  • 产量

Here is a list from Eloquent JavaScript book:

  • break
  • case
  • catch
  • class
  • const
  • continue
  • debugger
  • default
  • delete
  • do
  • else
  • enum
  • export
  • extend
  • false
  • finally
  • for
  • function
  • if
  • implements
  • import
  • in
  • instanceof
  • interface
  • let
  • new
  • null
  • package
  • private
  • protected
  • public
  • return
  • static
  • super
  • switch
  • this
  • throw
  • true
  • try
  • typeof
  • var
  • void
  • while
  • with
  • yield
反目相谮 2024-07-11 12:40:00

当前的答案都没有警告说,无论 ES-Dialect 如何,浏览器往往在 ES 规定的基础上拥有自己的保留关键字、方法等列表。

例如,IE9 禁止使用逻辑名称,例如:addFilterremoveFilter(它们是保留方法)。

请参阅http://www.jabcreations.com/blog/internet-explorer-9 获取特定于 IE9 的更广泛的“当前已知”列表。 我尚未在 msdn(或其他地方)上找到对它们的任何官方参考。

None of the current answers warn that regardless of ES-Dialect, browsers tend to have their own lists of reserved keywords, methods etc on top of what ES dictates.

For example, IE9 prohibits use of logical names like: addFilter, removeFilter (they, among others, are reserved methods).

See http://www.jabcreations.com/blog/internet-explorer-9 for a more extensive 'currently known' list specific to IE9. I have yet find any official reference to them on msdn (or elsewhere).

望笑 2024-07-11 12:39:58

这是一种与浏览器和语言版本无关的方法,用于确定 JavaScript 引擎是否将特定字符串视为关键字。 归功于 这个答案,它提供了解决方案的核心。

function isReservedKeyword(wordToCheck) {
    var reservedWord = false;
    if (/^[a-z]+$/.test(wordToCheck)) {
        try {
            eval('var ' + wordToCheck + ' = 1');
        } catch (error) {
            reservedWord = true;
        }
    }
    return reservedWord;
}

Here is a browser and language version agnostic way to determine if a particular string is treated as a keyword by the JavaScript engine. Credits to this answer which provides the core of the solution.

function isReservedKeyword(wordToCheck) {
    var reservedWord = false;
    if (/^[a-z]+$/.test(wordToCheck)) {
        try {
            eval('var ' + wordToCheck + ' = 1');
        } catch (error) {
            reservedWord = true;
        }
    }
    return reservedWord;
}
哽咽笑 2024-07-11 12:39:57

我刚刚在 JavaScript & 中读到了这一点。 jQuery:缺失的手册

并非所有这些保留字都会在所有浏览器中引起问题,但在命名变量时最好避开这些名称。

JavaScript 关键字: break、case、catch、 continue、debugger、default、delete、do、else、false、finally、for、function、if、in、instanceof、new、 null、return、switch、this、throw、true、try、typeof、var、void、while、with

保留供将来使用: abstract、boolean、byte、char、class、const、double、enum、export、extends、final、float、goto、implements、import、int、接口、let、长、本机、包、私有、受保护、公共、短、静态、超级、同步、抛出、瞬态、易失性、产量

浏览器中预定义的全局变量: alert、blur、close、document、focus、frames、history、innerHeight、innerWidth、length、location、navigator、open、outerHeight、外部宽度、父级、屏幕、screenX、screenY、状态栏、窗口

I was just reading about this in JavaScript & jQuery: The Missing Manual:

Not all of these reserved words will cause problems in all browsers, but it’s best to steer clear of these names when naming variables.

JavaScript keywords: break, case, catch, continue, debugger, default, delete, do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, with.

Reserved for future use: abstract, boolean, byte, char, class, const, double, enum, export, extends, final, float, goto, implements, import, int, interface, let, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, yield.

Pre-defined global variables in the browser: alert, blur, closed, document, focus, frames, history, innerHeight, innerWidth, length, location, navigator, open, outerHeight, outerWidth, parent, screen, screenX, screenY, statusbar, window.

回忆追雨的时光 2024-07-11 12:39:56

要补充benc的答案,请参阅标准 ECMA-262。 这些是官方的保留字,但只有学究们才会为了尊重标准而忽略实施。 对于最流行的实现(即 Firefox 和 Internet Explorer)的保留字,请参阅 benc 的答案。

EMCAScript-262 中的保留字是关键字未来保留字NullLiteralBooleanLiteral ,其中关键字

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

未来保留字

abstract  export      interface  static
boolean   extends     long       super
byte      final       native     synchronized
char      float       package    throws
class     goto        private    transient
const     implements  protected  volatile
double    import      public 
enum      int         short

NullLiteral

null

BooleanLiteral

true
false

To supplement benc's answer, see Standard ECMA-262. These are the official reserved words, but only a pedant ignores the implementation to respect the standard. For the reserved words of the most popular implementations, that is firefox and internet explorer, see benc's answer.

The reserved words in EMCAScript-262 are the Keywords, Future Reserved Words, NullLiteral, and BooleanLiterals, where the Keywords are

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

the Future Reserved Word​s are

abstract  export      interface  static
boolean   extends     long       super
byte      final       native     synchronized
char      float       package    throws
class     goto        private    transient
const     implements  protected  volatile
double    import      public 
enum      int         short

the NullLiteral is

null

and the BooleanLiterals are

true
false
送你一个梦 2024-07-11 12:39:55

我们应该链接到实际的信息来源,而不仅仅是谷歌的热门搜索。

http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words

JScript 8.0:
http://msdn.microsoft.com/en-us/library/ttyab5c8.aspx

We should be linking to the actual sources of info, rather than just the top google hit.

http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words

JScript 8.0:
http://msdn.microsoft.com/en-us/library/ttyab5c8.aspx

不知所踪 2024-07-11 12:39:53

这是我的诗,其中包含 JavaScript 中的所有保留关键字,献给那些当下保持诚实、而不仅仅是试图得分的人:

Let this long package float, 
Goto private class if short.
While protected with debugger case,  
Continue volatile interface.
Instanceof super synchronized throw, 
Extends final export throws.  

Try import double enum?  
- False, boolean, abstract function, 
Implements typeof transient break!
Void static, default do,  
Switch int native new. 
Else, delete null public var 
In return for const, true, char
…Finally catch byte.

Here is my poem, which includes all of the reserved keywords in JavaScript, and is dedicated to those who remain honest in the moment, and not just try to score:

Let this long package float, 
Goto private class if short.
While protected with debugger case,  
Continue volatile interface.
Instanceof super synchronized throw, 
Extends final export throws.  

Try import double enum?  
- False, boolean, abstract function, 
Implements typeof transient break!
Void static, default do,  
Switch int native new. 
Else, delete null public var 
In return for const, true, char
…Finally catch byte.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文