JavaScript 中的保留关键字
保留哪些 JavaScript 关键字(函数名称、变量等)?
What JavaScript keywords (function names, variables, etc) are reserved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
保留哪些 JavaScript 关键字(函数名称、变量等)?
What JavaScript keywords (function names, variables, etc) are reserved?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
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.以下是 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
当前的答案都没有警告说,无论 ES-Dialect 如何,浏览器往往在 ES 规定的基础上拥有自己的保留关键字、方法等列表。
例如,IE9 禁止使用逻辑名称,例如:
addFilter
、removeFilter
(它们是保留方法)。请参阅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).
这是一种与浏览器和语言版本无关的方法,用于确定 JavaScript 引擎是否将特定字符串视为关键字。 归功于 这个答案,它提供了解决方案的核心。
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.
我刚刚在 JavaScript & 中读到了这一点。 jQuery:缺失的手册:
I was just reading about this in JavaScript & jQuery: The Missing Manual:
要补充benc的答案,请参阅标准 ECMA-262。 这些是官方的保留字,但只有学究们才会为了尊重标准而忽略实施。 对于最流行的实现(即 Firefox 和 Internet Explorer)的保留字,请参阅 benc 的答案。
EMCAScript-262 中的保留字是关键字、未来保留字、NullLiteral 和BooleanLiteral ,其中关键字是
未来保留字是
NullLiteral
,BooleanLiteral是
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
the Future Reserved Words are
the NullLiteral is
and the BooleanLiterals are
我们应该链接到实际的信息来源,而不仅仅是谷歌的热门搜索。
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
这是我的诗,其中包含 JavaScript 中的所有保留关键字,献给那些当下保持诚实、而不仅仅是试图得分的人:
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: