JavaScript資料類型判斷

來源:互聯網
上載者:User

JavaScript的資料

JavaScript的資料分為兩種:簡單資料和複雜資料。簡單資料包含number,string,boolean,undefined和null這五種;複雜資料只有一種即object。

萬能的typeof

我們先測試一下通過typeof來擷取單一資料型別。什麼也別說了,上代碼是王道:

// 擷取變數obj的資料類型  function getType(obj) {  return typeof (obj);  }  /*常量擷取類型*/  alert(getType(1)); //number  alert(getType("jeff wong")); //string  alert(getType(true)); //boolean  alert(getType(undefined)); //undefined  alert(getType(null)); //object  /*變數擷取類型*/  var num = 1;  var str = "jeff wong";  var flag = true;  var hell = undefined;  var none = null;  alert(getType(num)); //number  alert(getType(str)); //string  alert(getType(flag)); //boolean  alert(getType(hell)); //undefined  alert(getType(none)); //object  

正如你所看到的那樣,通過typeof運算子,前面四個單一資料型別完全在意料之中,但是typeof null卻返回object。應該注意到,null是null類型的唯一值,但null並不是object,具有null值的變數也並非object,所以直接通過typeof,並不能正確得到null類型。 要正確擷取單一資料型別,只要在getType的地方加點改進就可以了:

function getType(obj) {  return (obj === null) ? "null" : typeof (obj);  }   

接著來試一下複雜資料類型object:

function Cat() {  }  Cat.prototype.CatchMouse = function () {  //do some thing  }  // 擷取變數obj的資料類型  function getType(obj) {  return (obj === null) ? "null" : typeof (obj);  }  var obj = new Object();  alert(getType(obj)); //object  var func = new Function();  alert(getType(func)); //function  var str = new String("jeff wong");  alert(getType(str)); //object  var num = new Number(10);  alert(getType(num)); //object  var time = new Date();  alert(getType(time)); //object  var arr = new Array();  alert(getType(arr)); //object  var reg = new RegExp();  alert(getType(reg)); //object  var garfield = new Cat();  alert(getType(garfield)); //object  

我們看到,除了Function(請注意大小寫)返回了function,不管是javascript的常見內建對象Object,String或者Date等等,還是自訂function,通過typeof返回的無一例外,通通都是object。但是對於自訂function,我們更願意得到它的“廬山真面目”(樣本中即Cat,而非object),而顯然,typeof不具備這種轉換處理能力。

constructor,想大聲說愛你

既然萬能的typeof也有無解的時候,那麼我們怎麼判斷一個變數是否是自訂的function執行個體呢?我們知道,javascript的所有對象都有一個constructor屬性,這個屬性可以幫我們判斷object資料類型,尤其是對自訂function同樣適用:

var obj = "jeff wong";  alert(obj.constructor == String); //true  obj = new Cat();  alert(obj.constructor == Cat); //true  

但是,下面的代碼您也可以測試一下:

//alert(1.constructor); //數字常量 出錯 數字常量無constructor  var num = 1;  alert(num.constructor == Number); //true  alert("jeff wong".constructor == String); //true  var str = "jeff wong";  alert(str.constructor == String); //true  var obj= null;  alert(obj.constructor); //null沒有constructor屬性  none = undefined;  alert(obj.constructor); //undefined沒有constructor屬性  

實驗證明,數字型常量,null和undefined都沒有constructor屬性。

下面的代碼或許還能有點啟發和挖掘作用:

function Animal() {  }  function Cat() {  }  Cat.prototype = new Animal();  Cat.prototype.CatchMouse = function () {  //do some thing  }  var obj = new Cat();  alert(obj.constructor == Cat); //false  ??  alert(obj.constructor == Animal); //true 理解  

原來對於原型鏈繼承的情況,constuctor也不那麼好使了。那怎麼辦?

直觀的instanceof

嘿嘿,有請instanceof隆重登場。看它的命名,好像是擷取某一個對象的執行個體,也不知這樣理解對不對?不管怎樣,我們還是動手改進上面的代碼測試一下先:

function Animal() {  }  function Cat() {  }  Cat.prototype = new Animal();  Cat.prototype.CatchMouse = function () {  //do some thing  }  var garfield = new Cat();  alert(garfield instanceof Cat); //true 毫無疑問  alert(garfield instanceof Animal); //true 可以理解  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.