保留了哪些JavaScript关键字(函数名,变量等)?
这是我的诗,其中包括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.
我们应该链接到实际的信息来源,而不仅仅是最热门的谷歌搜索.
http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words
JScript 8.0:http: //msdn.microsoft.com/en-us/library/ttyab5c8.aspx
要补充benc的答案,请参阅标准ECMA-262.这些是官方保留词,但只有学者忽略了尊重标准的实施.对于最受欢迎的实现的保留字,即firefox和Internet Explorer,请参阅benc的答案.
EMCAScript-262中的保留字是关键字 s,Future Reserved Word,NullLiteral和BooleanLiteral,其中关键字是
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
在未来的保留字 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
该NullLiteral是
null
和BooleanLiteral是
true false
我刚刚在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
.
这是一种浏览器和语言版本无关的方法,用于确定JavaScript引擎是否将特定字符串视为关键字.这个答案的功劳提供了解决方案的核心.
function isReservedKeyword(wordToCheck) { var reservedWord = false; if (/^[a-z]+$/.test(wordToCheck)) { try { eval('var ' + wordToCheck + ' = 1'); } catch (error) { reservedWord = true; } } return reservedWord; }