JavaScript權威指南——詞法結構(4)

標識符和保留字ide

一、標識符函數

標識符就是一個名字。在JavaScript中,標識符用來給變量、屬性、函數和參數進行命名,或者用作某些循環語句中的跳轉位置的標記。this

//變量 var identifier = 123; //屬性 (new Object).identifier = 'test'; //函數及參數 function indetifierName(identifier){}; //跳轉標記 identifier: for(var i = 0; i < 5; i++){ if(i == 3){ break identifier; } }

標識符遵照的規則:JavaScript標識符必須以字母、下劃線(_)和美圓符號($)開始,後續的字符能夠是字母、數字、下劃線和美圓符號。spa

二、保留字debug

JavaScript把一些標識符拿出來用作本身的關鍵字。所以,就不能再在程序中把這些關鍵字用作標識符了。code

下面列出JavaScript中的關鍵字:blog

if else switch case do while for in
try catch var this true false null delete
function return break continue default typeof instanceof throw
new void width finally debugger eval arguments  
let const class enum export extends import implements
interface yield super public private protected package  

 

 

 

 

 

 

 

 

 

 

 

JavaScript預約義了不少全局變量和函數,應當避免把它們的名字用作變量名和函數名:ip

Object String Number Boolean Array
Date Math RegExp Function Error
arguments NaN isNaN Infinity inInfinity
parseInt parseFloat undefined JSON eval
SyntaxError TypeError ReferenceError RangeError URIError
EvalError encodeURI encodeURIComponent decodeURI decodeURIComponent

 

 

 

 

 

 

ES3將Java的全部關鍵字都列爲本身的保留字,儘管這些保留字的ES5中放寬了限制,但若是你但願代碼能在基於ES3實現的解釋器上運行的話,應當避免使用這些關鍵字做爲標識符。it

相關文章
相關標籤/搜索