Hướng dẫn reserved keywords trong javascript

- Từ dành riêng là những từ hệ thống, ta không thể sử dụng chúng để đặt tên cho biến, hàm, đối tượng, ....

- Dưới đây là danh sách các từ dành riêng:

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

- Nên tránh sử dụng tên của các đối tượng, thuộc tính, phương thức được xây dựng sẵn:

Array Date eval function
hasOwnProperty Infinity isFinite isNaN
isPrototypeOf length Math NaN
name Number Object prototype
String toString undefined valueOf

- JavaScript có thể được sử dụng như một ngôn ngữ lập trình trong nhiều ứng dụng. Do đó, chúng ta cũng nên tránh sử dụng tên của các đối tượng và thuộc tính của HTML và 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

Trong JavaScript bạn không thể dùng các từ dành riêng sau như các biến, nhãn hoặc các tên hàm:

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

Từ được đánh dấu với* là mới trong ECMAScript 5 và 6.

Bạn có thể đọc thêm về sự khác biệt về các phiên bản JavaScript trong chương Các phiên bản của JavaScript(JavaSript Versions).

Các từ dành riêng bị loại bỏ

Các từ dành riêng sau đã bị loại bỏ khỏi tiêu chuẩn ECMAScript 5/6: 

abstract boolean byte char
double final float goto
int long native short
synchronized throws transient volatile

Đừng dùng các từ này như các biến. ECMASCript 5/6 không hỗ trợ trong tất cả các trình duyệt.

Các phương thức, thuộc tính và đối tượng của JavaScript

Bạn cũng nên tránh sử dụng tên của các đối tượng, các thuộc tính, và các phương thức được xây dựng bên trong của JavaScript:

Array Date eval function
hasOwnProperty Infinity isFinite isNaN
isPrototypeOf length Math NaN
name Number Object prototype
String toString undefined valueOf

JavaScript thường được dùng chung với Java. Bạn nên tránh sử dụng một vài đối tượng, và thuộc tính Java như các định danh JavaScript:

getClass java JavaArray javaClass
JavaObject JavaPackage

Các từ dành riêng khác

JavaScript có thể được dùng như một ngôn ngư lập trình trong nhiều ứng dụng.

Bạn cũng nên tránh sử dụng tên của HTML và các đối tượng Window và các thuộc tính:

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


Trình xử lý sự kiên HTML(HTML Event Handlers)

Ngoài ra bạn nên tránh sử dụng vào tên của tất cả các trình xử lý sự kiện HTML.

Ví dụ:

onblur onclick onerror onfocus
onkeydown onkeypress onkeyup onmouseover
onload onmouseup onmousedown onsubmit