1. 標識符:在JS中所有的可以有我們自主命名的都可以稱為是標識符。
例如:變量名、函數名、屬性名都屬於標識符。
2. JavaScript語言中標識符的命名規則:
① 區分大小寫,Myname與myname是兩個不同的標識符。
② 標識符首字符可以是以下划線(_)、美元符($)或者字母開始,不能是數字。
③標識符中其它字符可以是下划線(_)、美元符($)、字母或數字組成的。
例: userName,User_Name,_sys_val,身高,$change等均為合法的標識符,而2mail,room#,class為非法的標識符。其中,使用中文“身高”命名的變量是合法的。
注意:JavaScript中的字母是采用Unicode,Unicode叫做統一編碼制,是國際上通用的16位編碼制,它包含了亞洲文字編碼,如中文、日文、韓文等字符。所有JavaScript中的字母可以是中文、日文和韓文等亞洲字母。
4. 保留字:保留字是語言中定義具有特殊含義的標識符,保留字不能作為標識符使用。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 版本的內容。
- 8. 被刪除的保留詞
- 以下保留詞已被從 ECMAScript 5/6 標准中刪除:
abstract |
boolean |
byte |
char |
double |
final |
float |
goto |
int |
long |
native |
short |
synchronized |
throws |
transient |
volatile |
- 請不要將這些保留詞用作變量。並非所有瀏覽器都完全支持 ECMAScript 5/6。
11. JavaScript 對象、屬性和方法
- 您還應該避免使用 JavaScript 內建對象的名稱、屬性和方法:
Array |
Date |
eval |
function |
hasOwnProperty |
Infinity |
isFinite |
isNaN |
isPrototypeOf |
length |
Math |
NaN |
name |
Number |
Object |
prototype |
String |
toString |
undefined |
valueOf |
13. Java 保留詞
- JavaScript 常與 Java 一起使用。您應該避免把某些 Java 對象和屬性用作 JavaScript 標識符:
getClass |
java |
JavaArray |
javaClass |
JavaObject |
JavaPackage |
15. 其他保留詞
- 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 |
18. HTML 事件處理程序
- 此外您應該避免使用所有 HTML 事件處理程序的名稱。
- 例如:
onblur |
onclick |
onerror |
onfocus |
onkeydown |
onkeypress |
onkeyup |
onmouseover |
onload |
onmouseup |
onmousedown |
onsubmit |