在JavaScript中,创建变量或函数时,不可以使用保留词,保留词在JavaScript中有特殊作用意义。常用的保留词如下所示:
保留词 | 保留词 | 保留词 | 保留词 |
---|---|---|---|
abstract | arguments | await | boolean |
break | byte | case | catch |
char | class | const | continue |
debugger | default | delete | do |
double | else | enum | eval |
export | extends | FALSE | final |
finally | float | for | function |
goto | if | implements | import |
in | instanceof | int | interface |
let | long | native | new |
null | package | private | protected |
public | return | short | static |
super | switch | synchronized | this |
throw | throws | transient | TRUE |
try | typeof | var | void |
volatile | while | with | yield |
因为JavaScript中可能会用到Java插件,因此选择变量名和函数名时,也尽量避开Java方面的保留词:
保留词 | 保留词 | 保留词 | 保留词 |
---|---|---|---|
getClass | java | JavaArray | javaClass |
JavaObject | JavaPackage |
JavaScript也要避开HTML对象以及window对象的各种属性名与方法名。
保留词 | 保留词 | 保留词 | 保留词 |
---|---|---|---|
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 |
JavaScript也要避开HTML事件相关的保留词。
保留词 | 保留词 | 保留词 | 保留词 |
---|---|---|---|
onblur | onclick | onerror | onfocus |
onkeydown | onkeypress | onkeyup | onmouseover |
onload | onmouseup | onmousedown | onsubmit |