在 JavaScript 中,一些标识符是保留关键字,不能用作变量名或函数名。
JavaScript 标准
所有的现代浏览器完全支持 ECMAScript3(ES3,JavaScript 的第三版,从1999年开始)
ECMAScript 4(ES4)未通过
ECMAScript 5(ES5,2009 年发布),随着时间的推移,我们开始看到,所有的现代浏览器已经完全支持 ES5
ECMAScript 6(ES6),已经在2015年6月正式发布了。它的目标,是使得 JavaScript 语言可以用来编写复杂的大型应用程序,成为企业级开发语言
JavaScript 中最重要的保留字(按字母顺序):
Javascript 的保留关键字不可以用作变量、标签或者函数名。有些保留关键字是作为 Javascript 以后扩展使用
abstract |
else |
instanceof |
super |
boolean |
enum |
int |
switch |
break |
export |
interface |
synchronized |
byte |
extends |
let |
this |
case |
false |
long |
throw |
catch |
final |
native |
throws |
char |
finally |
new |
transient |
class |
float |
null |
true |
const |
for |
package |
try |
continue |
function |
private |
typeof |
debugger |
goto |
protected |
var |
default |
if |
public |
void |
delete |
implements |
return |
volatile |
do |
import |
short |
while |
double |
in |
static |
with |
JavaScript 对象、属性和方法
也应该避免使用 JavaScript 内置的对象、属性和方法的名称作为 Javascript 的变量或函数名
MathNaNnameNumberObjectprototypeStringtoStringundefinedvalueOf
Java 保留关键字
JavaScript 经常与 Java 一起使用。您应该避免使用一些 Java 对象和属性作为 JavaScript 标识符
getClassjavaJavaArrayjavaClassJavaObjectJavaPackage
Windows 保留关键字
JavaScript 可以在 HTML 外部使用。它可在许多其他应用程序中作为编程语言使用。
在 HTML 中,您必须(为了可移植性,您也应该这么做)避免使用 HTML 和 Windows 对象和属性的名称作为 Javascript 的变量及函数名
alert |
all |
anchor |
anchors |
area | assign |
blur |
button |
checkbox | clearInterval |
clearTimeout |
clientInformation |
close | closed |
confirm |
constructor |
crypto |
decodeURI |
decodeURIComponent |
defaultStatus |
document |
element |
elements |
embed |
embeds |
encodeURI |
encodeURIComponent |
escape |
event | fileUpload |
focus |
form |
forms |
frame |
innerHeight |
innerWidth |
layer |
layers |
link |
location |
mimeTypes |
navigate |
navigator |
frames |
frameRate |
hidden |
history |
image |
images |
offscreenBuffering |
open |
opener |
option |
outerHeight |
outerWidth |
packages |
pageXOffset |
pageYOffset |
parent | parseFloat |
parseInt |
password |
pkcs11 |
plugin |
prompt |
propertyIsEnum |
radio |
reset |
screenX |
screenY |
scroll |
secure |
select |
self |
setInterval |
setTimeout |
status |
submit |
taint |
text |
textarea |
top |
unescape |
untaint |
window |
HTML 事件句柄
除此之外,您还应该避免使用 HTML 事件句柄的名称作为 Javascript 的变量及函数名
onblur |
onclick |
onerror |
onfocus |
onkeydown |
onkeypress |
onkeyup |
onmouseover |
onload |
onmouseup |
onmousedown |
onsubmit |
非标准 JavaScript
除了保留关键字,在 JavaScript 实现中也有一些非标准的关键字
一个实例是 const 关键字,用于定义变量。 一些 JavaScript 引擎把 const 当作 var 的同义词。另一些引擎则把 const 当作只读变量的定义
Const 是 JavaScript 的扩展。JavaScript 引擎支持它用在 Firefox 和 Chrome 中