JavaScript數(shù)據(jù)類型判斷

JavaScript的數(shù)據(jù)

? ? ? ? JavaScript的數(shù)據(jù)分為兩種:簡單數(shù)據(jù)和復雜數(shù)據(jù)。簡單數(shù)據(jù)包含number,string,boolean,undefined和null這五種;復雜數(shù)據(jù)只有一種即object。

萬能的typeof

我們先測試一下通過typeof來獲取簡單數(shù)據(jù)類型。什么也別說了,上代碼是王道:

// 獲取變量obj的數(shù)據(jù)類型

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運算符,前面四個簡單數(shù)據(jù)類型完全在意料之中,但是typeof null卻返回object。應該注意到,null是null類型的唯一值,但null并不是object,具有null值的變量也并非object,所以直接通過typeof,并不能正確得到null類型。 要正確獲取簡單數(shù)據(jù)類型,只要在getType的地方加點改進就可以了:

function getType(obj) {

return (obj === null) ? "null" : typeof (obj);

}

接著來試一下復雜數(shù)據(jù)類型object:

function Cat() {

}

Cat.prototype.CatchMouse = function () {

//do some thing

}

// 獲取變量obj的數(shù)據(jù)類型

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的常見內(nèi)置對象Object,String或者Date等等,還是自定義function,通過typeof返回的無一例外,通通都是object。但是對于自定義function,我們更愿意得到它的“廬山真面目”(示例中即Cat,而非object),而顯然,typeof不具備這種轉(zhuǎn)換處理能力。

constructor,想大聲說愛你

? ? ? ?既然萬能的typeof也有無解的時候,那么我們怎么判斷一個變量是否是自定義的function實例呢?我們知道,javascript的所有對象都有一個constructor屬性,這個屬性可以幫我們判斷object數(shù)據(jù)類型,尤其是對自定義function同樣適用:

var obj = "jeff wong";

alert(obj.constructor == String); //true

obj = new Cat();

alert(obj.constructor == Cat); //true

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

//alert(1.constructor); //數(shù)字常量 出錯 數(shù)字常量無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屬性

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

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

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 可以理解


原文轉(zhuǎn)載:http://www.nowamagic.net/librarys/veda/detail/531

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • JS基礎(chǔ)講解 JavaScript組成ECMAScript:解釋器、翻譯DOM:Document Object M...
    FConfidence閱讀 621評論 0 1
  • 第三章 基本概念 3.1 語法 ECMAScript標識符一般采用駝峰大小寫格式,也就是第一個字母小寫,剩下的每個...
    小雄子閱讀 655評論 0 1
  • 第一章: JS簡介 從當初簡單的語言,變成了現(xiàn)在能夠處理復雜計算和交互,擁有閉包、匿名函數(shù), 甚至元編程等...
    LaBaby_閱讀 1,751評論 0 6
  • 工廠模式類似于現(xiàn)實生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實現(xiàn)同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 8,110評論 2 17
  • 學習上倒是還湊合。昨晚早早地躺在床上寫作文,這是一篇關(guān)于真誠的作文,開始寫時只有一個基本的想法,或許是記敘文體,寫...
    張先友閱讀 176評論 0 0

友情鏈接更多精彩內(nèi)容