JavaScript中的保留关键字

tit*_*ous 161 javascript reserved-words

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

小智 1468

这是我的诗,其中包括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.
Run Code Online (Sandbox Code Playgroud)

  • 这是es6时间所以我等待你的更新 (127认同)
  • https://screenshots.firefox.com/MVw02wDrHPi197kK/stackoverflow.com它让我不想点击那个 (5认同)

ben*_*enc 110

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

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

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


Jos*_*ten 61

要补充benc的答案,请参阅标准ECMA-262.这些是官方保留词,但只有学者忽略了尊重标准的实施.对于最受欢迎的实现的保留字,即firefox和Internet Explorer,请参阅benc的答案.

EMCAScript-262中的保留字是关键字 s,Future Reserved Word,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
Run Code Online (Sandbox Code Playgroud)

未来的保留字 s的

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
Run Code Online (Sandbox Code Playgroud)

NullLiteral

null
Run Code Online (Sandbox Code Playgroud)

BooleanLiteral

true
false
Run Code Online (Sandbox Code Playgroud)

  • 这是什么答案.它甚至没有押韵. (12认同)
  • 找到了!它在ES3中作为未来的保留词以及其他一长串列表出现,但它在ES5中被删除了. (2认同)

its*_*_me 21

我刚刚在JavaScript和jQuery中阅读了这篇文章:The Missing Manual:

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

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, interface, let, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, yield.

浏览器中预定义的全局变量: alert, blur, closed, document, focus, frames, history, innerHeight, innerWidth, length, location, navigator, open, outerHeight, outerWidth, parent, screen, screenX, screenY, statusbar, window.

  • "保留供将来使用"::所有Java词......真的很懒. (2认同)
  • 请注意,"保留"与"预先初始化"不同.在浏览器中,`alert`已经初始化,但没有什么能阻止你重新分配`alert = 5`.但是,您不能将`window`设置为5,但可以将其用作局部变量.使用保留关键字,将来使用,`null`,`false`,`true`是不可能的. (2认同)

GOT*_*O 0 5

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

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

  • 这对于在构建时运行的测试用例来说是完美的,只要它不是您在运行时公开的内容就完全有效. (8认同)
  • 如果你必须使用"eval"来做任何事情,那很可能意味着你做错了. (7认同)