JavaScript 保留字
本页整理了 JavaScript 的保留字,这些保留字不能用于变量名称、函数名称或者标签名称;还整理了一些不建议使用的名称,避免引入错误。
本页整理了 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 |
标有 *
的单词是 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 对象、属性和方法
您还应该避免使用 JavaScript 内置对象、属性和方法的名称:
Array |
Date |
eval |
function |
hasOwnProperty |
Infinity |
isFinite |
isNaN |
isPrototypeOf |
length |
Math |
NaN |
name |
Number |
Object |
prototype |
String |
toString |
undefined |
valueOf |
其他保留字
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 事件处理程序
此外,您应该避免使用所有 HTML 事件处理程序的名称。例如:
onblur |
onclick |
onerror |
onfocus |
onkeydown |
onkeypress |
onkeyup |
onmouseover |
onload |
onmouseup |
onmousedown |
onsubmit |