淺玩JavaScript的資料類型判斷

來源:互聯網
上載者:User

標籤:instance   資料   nan   pre   microsoft   argument   存在   date   lock   

前言

平常在需要進行類型判斷時,隨手拿起typeof就像手槍一樣只管突突突...也沒有仔細的去瞭解它的具體特性。
所以這裡就利用空閑時間,來做一個較為詳細的瞭解。

首先我們來全面看一遍typeof類型的檢測情況:

這裡我給一組比較詳細的資料類型變數:

 1     var s = new String(‘abc‘), 2         n = 123, 3         b = true, 4         a = new Array(), 5         o = new Object(), 6         d = new Date(), 7         u = undefined, 8         f = function(){}, 9         w = window,10         doc = document,11         r = new RegExp(),12         m = Math,13         t = window.setTimeout,14         _s = ‘123‘,15         _n = null,16         _d = document.body,17         _nan = NaN;

下面我們用typeof分別來檢測這些變數的資料類型,看看具體的返回結果:

typeof s --> stringtypeof n --> numbertypeof b --> booleantypeof a --> objecttypeof o --> objecttypeof d --> objecttypeof u --> undefinedtypeof f --> functiontypeof w --> objecttypeof doc --> objecttypeof r --> objecttypeof m --> objecttypoef t --> functiontypeof _s --> numbertypoef _n --> objecttypeof _d --> objecttypeof _nan --> number

通過以上測試,可以很肯定的說typeof最多隻可檢查5種資料類型:  string,number,boolean,underfined,object  。
實際上JavaScript也只有這5種資料類型。
像Date、Array、Function、HTMLElement、Math、RegExp這些資料,在JavaScript中,他們都是Object對象的再次封裝,所以用typeof檢測它們就會返回 object 類型。
但是在實際的運用中,如果將 Date、Array、Function、HTMLElement、Math、RegExp 都作為object類型看待,就很不切實際。
幸運的是,我們可以從這些對象的建構函式(constructor)上獲得到代表它們具體的含義的資訊。

例如: 

a.constructor.toString() -- > "function Array() { [native code] }";

所以通過結合typeof、Object.constructor 這兩種方法,自己編寫了一個檢測類型的方法:

 1 function getType(){ 2  3     var value = arguments[0], 4         type = function(){ 5             return /\w+\s(\w+)\(\)\s.+?/.test(value.constructor.toString()) ? RegExp.$1.toLowerCase() : ‘arguments except‘;  6         }; 7  8     if(typeof value){ 9         return value !== null ?  value !== undefined ? type() : ‘undefined‘ :  ‘null‘;10     }else{11         return ‘undefined‘;12     }13 14 }

另外對於typeof在使用上還有一個小技巧。用typeof檢測一個不存在“變數”的資料類型時,並不會報錯,而是返回undefined。
而在以前,對與不確定的代碼,我們都是通過try{}catch(e){...} 進行嘗試啟動並執行。

最後在查閱typeof的資料時。順便也簡單的看了下 instanceof 運算子的介紹。
總的來說,instanceof運算子是用於“檢測左側參數是否是右側參數的一個執行個體”。如果是,則返回true,否則返回false。
樣本:

1 var p = new person();2 p instanceof person // --> true;

在這個樣本中因為p是建構函式的person的一個執行個體,所以通過instanceof運算子得到的就是true。
這一點,可以在 p.constructor == ‘function person(){}‘  得到應徵。
再稍微複雜的還有:

1 function person(){}2 function animal(){}3 4 persion.prototype = new animal();5 var p = new person();6 p instanceof persion() //--> true7 p instanceof animal() //--> true

 

---------------- 好了... 睡覺去.... --------- 明天繼續奮鬥!!!! ---------------

淺玩JavaScript的資料類型判斷

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.