跳到主要內容

[鐵人賽] Null & undefined 型態差異 - Node.js Day 7

Null & undefined 型態差異

空無是一種很奇妙的狀態,在 JavaScript 裡面,null, undefined 是一種奇妙的東西。今天來探討什麼是 null ,什麼是 undefined.

null

變數要經過宣告,賦予 null ,才會形成 null 型態。

var a=null;

null 在 JavaScript 中表示一個空值。

undefined

從字面上就表示目前未定義,只要一個變數在初始的時候未給予任何值的時候,就會產生 undefined

var a;

console.log(a);

// print : undefined

這個時候 a 就是屬於 undefined 的狀態。

另外一種狀況就是當 Object 被刪除的時候。

var a = {};

delete a;

console.log(a);

//print: undefined.

Object 在之後會介紹,先記住有這個東西。而使用 delete 的時候,就可以讓這個 Object 被刪除,就會得到結果為 undefined.

兩者比較

null, undefined 在本質上差異並不大,不過實質上兩者並不同,如果硬是要比較,建議使用 === 來做為判斷標準,避免 null, undefined 這兩者被強制轉型。

var a=null,
    b;

if (a === b) {
    console.log('same');
} else {
    console.log('different');
}

//print: different

從 typeof 也可以看到兩者本質上的差異,

typeof null;
//print: 'object'

typeof undefined;
//print: 'undefined'

null 本質上是屬於 object, 而 undefined 本質上屬於 undefined ,意味著在 undefined 的狀態下,都是屬於未定義。

如果用判斷式來決定,會發現另外一種狀態

Boolean(null);
// false

Boolean(undefined);
// false

可以觀察到,如果一個變數值為 null, undefined 的狀態下,都是屬於 false。

這樣說明應該幫助到大家了解,其實要判斷一個物件、屬性是否存在,只需要使用 if

var a;

if (!a) {
    console.log('a is not existed');
}

//print: a is not existed

a 為 undefined 由判斷式來決定,是屬於 False 的狀態。

後記

在 Node.js 開發中比較少會碰到 null, undeinfed 的狀況,只要判斷都是使用 === ,不用偷工減料,讓前後端判斷標準一致,就可以完整判斷目前的數值型態為何。

在這邊提出 null, undefined 是為了讓大家更為了解 JavaScript 本身的一些謬誤,變數的部分到這邊一個段落結束,明天會從 Array 開始談起。

留言

這個網誌中的熱門文章

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。

[教學] 快快樂樂刪除CodeIgniter index.php

預設的CI網址預設都設定為index.php同一層級,因此所有的程式都必須指定index.php導向才能開始,例如 http://localhost/ci/index.php/welcome/test http://localhost/ci/welcome/test 本文將說明如何將惱人的index.php消除,還你一個漂亮的URL。 設定開始: 接下來說明如何使用rewrite方式將惱人的index.php去除。 rewrite不清楚的人,煩請先自行google 首先要先確定Apache的 mod_rewrite 有 開啟 ,如果沒有開啟請設定好之後重新啟動apache。 接著,在根目錄底下建立一個新檔案,檔名為 .htaccess ,裡面程式碼如下: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 接著到 application/config/config.php ,開啟檔案修改 $config['index_page'] = ""; 注意: /index.php/$1 要根據你目錄,例如 http://localhost/index.php ,網站根目錄為 /ci/index.php 則要寫成 /ci/index.php/$1 接著至CI目錄下,尋找 config\config.php , 修改一下裡面的檔案,修改如下: $config['index_page'] = ""; 存檔後,如此一來大功告成。 參考資料 官方網站說明

[教學] Mojito 安裝與入門,install mojito for beginner.

mojito ,最近終於從 YDN 對外公開此專案,這個套件主要用於解決前端多重裝置及瀏覽器端的問題,後端服務採取 node.js ,因此使用上必定要先準備以下幾個元素 準備素材 c++ complier git Node.js > 0.4.x NPM > 1.0.x 安裝方式 git clone git://github.com/yahoo/mojito.git cd mojito/source sudo npm install -g . npm install . 以上四個簡單的步驟,就可以把 mojito module 完整安裝到服務器上,接著就可以開始進入 mojito 的世界 使用方式 mojito 提供了完整的 command line 給開發者使用,接著先建立一個基本 hello world 專案,跟著以下步驟完成第一個專案。首先建立一個 mojito application, mojito create app hello cd hello 切換到目錄之後,再接著建立自己的 mojit,這邊的 mojit 就像是一個應用(application)可能會包含許多個獨立網站體,擁有獨立架構的 MVC ,包含內部設定等,詳細資料可以參考官方的 說明 ,建立 mojit mojito create mojit HelloMojit 輸入指令後,會看到顯示結果如下, creating mojit called 'HelloMojit' (using "default" archetype) ✔ mojit: HelloMojit created! ✔ mojito done. 接著修改 application.json 這個 mojit 設定檔案,讓剛才新建立的 hellp application 指定底下有一個 mojit -> HelloMojit ,讓應用可以去執行 mojit controller ,修改如下, [ { "settings": [ "master" ], "appPort": 8666, "specs": { ...