from http://blog.xuite.net/ahdaa/blog1/34061193
這兩個是Javascript的特殊資料類型
null |
意指『沒有值』。被視為一個物件(object)。 |
1 |
<script type= "text/javascript" > |
|
undefined |
有以下兩種情形會被視為undefined: 1. 該變數未被宣告、 2. 被宣告的變數未給值。(object)。 |
1 |
<script type= "text/javascript" > |
2 |
alert( typeof (undefined)); |
|
先宣告兩變數objNull以及objUndef
1 |
<script type= "text/javascript" > |
3 |
var objUndef = undefined; |
接下來進行一串輸出測試
01 |
<script type= "text/javascript" > |
07 |
alert(objNull + objUndef); |
再來將兩個變數各別加上123的數值類型以及變數類型
01 |
<script type= "text/javascript" > |
04 |
alert(objNull + "123" ); |
06 |
alert(objUndef + 123); |
08 |
alert(objUndef + "123" ); |
最後再輸出一個沒有被宣告的變數objUnDeclare以及被宣告沒給值的變數objDeclare
1 |
<script type= "text/javascript" > |
6 |
alert( typeof (objDeclare)); |
7 |
alert( typeof (objUnDeclare)); |
從這個結果看來,
typeof(objUnDeclare)還頗適合拿來做為網頁間傳遞參數的判斷,
判斷該變數被傳遞過來、有否被宣告以及是否被宣告而沒給值後,再來做下一步的程式執行!