在 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 |
标有 * 的单词是 ECMAScript 5 和 6 中的新单词。
您可以在 JS 版本一章中阅读有关不同 JavaScript 版本的更多信息。
以下保留字已从 ECMAScript 5/6 标准中删除:
abstract | boolean | byte | char |
double | final | float | goto |
int | long | native | short |
synchronized | throws | transient | volatile |
不要将这些词用作变量。 ECMAScript 5/6 没有完全支持 在所有浏览器中。
您还应该避免使用 JavaScript 内置对象、属性和方法的名称:
Array | Date | eval | function |
hasOwnProperty | Infinity | isFinite | isNaN |
isPrototypeOf | length | Math | NaN |
name | Number | Object | prototype |
String | toString | undefined | valueOf |
JavaScript 通常与 Java 一起使用。你应该避免使用一些Java对象 和属性作为 JavaScript 标识符:
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 |
此外,您应该避免使用所有 HTML 事件处理程序的名称。
例子:
onblur | onclick | onerror | onfocus |
onkeydown | onkeypress | onkeyup | onmouseover |
onload | onmouseup | onmousedown | onsubmit |