JavaScript - 保留關鍵字



JavaScript 中的保留關鍵字

JavaScript 中的保留關鍵字是預定義的關鍵字,用於為程式語言的內建功能提供服務。例如,varlet 關鍵字用於定義變數,function 關鍵字用於定義函式等。JavaScript 包含 50 多個保留關鍵字。

簡單來說,不能將保留關鍵字用作識別符號。如果這樣做,將會發生衝突,程式碼將生成錯誤的輸出或丟擲錯誤。

例如,以下程式碼將丟擲錯誤,因為“function”用作識別符號。

var function = "Hello";

保留關鍵字

以下是保留關鍵字列表;不能將它們用作識別符號 -

abstract doubleimplements return
argumentselse in switch
await enum instanceof synchronized
boolean eval int this
break export interface throw
byte extends let throws
case false long transient
catch final native true
char finally new try
class float null typeof
const for package var
continue function private void
debugger goto protected volatile
default if public yield
delete implements short while
do import static with
double in super

ES5 和 ES6 中新增的保留關鍵字

在 ES5 和 ES6 版本的 JavaScript 中添加了一些新的關鍵字。但是,有些目前正在使用,有些關鍵字為將來的版本保留。

await class enum export
extends import let Super

已刪除的保留關鍵字

一些保留關鍵字已從 JavaScript 中刪除,不能使用它們來實現特定的功能。但是,仍然不能將以下關鍵字用作識別符號,因為許多瀏覽器不支援它們。

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

JavaScript 物件、屬性和方法

不應將 JavaScript 內建物件、屬性和方法名稱用作識別符號。

JavaScript 內建物件

Array ArrayBuffer Boolean DataView
Date Error eval Float32Array
Float64Array Function Generator GeneratorFunction
Int8Array Int16Array Int32Array Intl
JSON Map Math Number
Object Promise Proxy RangeError
ReferenceError Reflect RegExp Set
String Symbol SyntaxError TypeError
Uint8Array Uint8ClampedArray Uint16Array Uint32Array
URIError WeakMap WeakSet

JavaScript 內建屬性

length constructor prototype __proto__ caller callee

JavaScript 方法

toString shift indexOf split
toLocaleString unshift lastIndexOf substr
valueOf slice includes substring
toLocaleDateString splice isArray toLowerCase
toLocaleTimeString sort from toLocaleLowerCase
toLocaleString forEach of toUpperCase
toFixed map charAt toLocaleUpperCase
toExponential filter charCodeAt trim
toPrecision reduce codePointAt startsWith
concat reduceRight normalize endsWith
join every repeat match
pop some replace test
push find search reverse
findIndex slice

但是,可以探索更多 JavaScript 內建方法,並避免將它們用作識別符號。

其他保留關鍵字

JavaScript 可以與其他程式語言(如 HTML、Java 等)一起使用。因此,您也應該避免使用 HTML、Java 等中保留的關鍵字。

以下是其他保留關鍵字的列表,其中大部分是“window”物件的屬性。

alert elements frameRate radio
all embed hidden reset
anchor embeds history screenX
anchors encodeURI image screenY
area encodeURIComponent images scroll
assign escape offscreenBuffering secure
blur event open select
button fileUpload opener self
checkbox focus option setInterval
clearInterval form outerHeight setTimeout
clearTimeout forms outerWidth status
clientInformation frame packages submit
close innerHeight pageXOffset taint
closed innerWidth pageYOffset text
confirm layer parent textarea
constructor layers parseFloat top
crypto link parseInt unescape
decodeURI location password untaint
decodeURIComponent mimeTypes pkcs11 window
defaultStatus navigate plugin document
navigator prompt element frames
propertyIsEnum

HTML 事件處理器

您不應該在 JavaScript 中使用 HTML 事件處理器作為變數名。

這裡,我們列出了一些事件處理器。

onclick ondblclick onmouseover onmouseout
onmousemove onkeydown onkeyup onkeypress
onfocus onblur onchange onsubmit
onreset onload onunload onresize
onscroll

簡而言之,您應該避免將以上所有關鍵字用作變數或函式名。

廣告