|
Javascript的數(shù)據(jù)分為兩種:簡(jiǎn)單數(shù)據(jù)和復(fù)雜數(shù)據(jù)。簡(jiǎn)單數(shù)據(jù)包含number,string,boolean,undefined和null這五種;復(fù)雜數(shù)據(jù)只有一種即object。【此處友情鳴謝李戰(zhàn)老師,<<悟透JavaScript>>寫(xiě)得太傳神,印象太深刻了】
二、Javascript的數(shù)據(jù)類型檢測(cè)
1、萬(wàn)能的typeof
我們先測(cè)試一下通過(guò)typeof來(lái)獲取簡(jiǎn)單數(shù)據(jù)類型。什么也別說(shuō)了,上代碼是王道:
復(fù)制代碼 代碼如下:
// 獲取變量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
正如你所看到的那樣,通過(guò)typeof運(yùn)算符,前面四個(gè)簡(jiǎn)單數(shù)據(jù)類型完全在意料之中,但是typeof null卻返回object。應(yīng)該注意到,null是null類型的唯一值,但null并不是object,具有null值的變量也并非object,所以直接通過(guò)typeof,并不能正確得到null類型。 要正確獲取簡(jiǎn)單數(shù)據(jù)類型,只要在getType的地方加點(diǎn)改進(jìn)就可以了:
復(fù)制代碼 代碼如下:
function getType(obj) {
return (obj === null) ? "null" : typeof (obj);
}
接著來(lái)試一下復(fù)雜數(shù)據(jù)類型object:
復(fù)制代碼 代碼如下:
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(請(qǐng)注意大小寫(xiě))返回了function,不管是Javascript的常見(jiàn)內(nèi)置對(duì)象Object,String或者Date等等,還是自定義function,通過(guò)typeof返回的無(wú)一例外,通通都是object。但是對(duì)于自定義function,我們更愿意得到它的“廬山真面目”(示例中即Cat,而非object),而顯然,typeof不具備這種轉(zhuǎn)換處理能力。
2、constructor,想大聲說(shuō)愛(ài)你
既然萬(wàn)能的typeof也有無(wú)解的時(shí)候,那么我們?cè)趺磁袛嘁粋€(gè)變量是否是自定義的function實(shí)例呢?我們知道,Javascript的所有對(duì)象都有一個(gè)constructor屬性,這個(gè)屬性可以幫我們判斷object數(shù)據(jù)類型,尤其是對(duì)自定義function同樣適用:
復(fù)制代碼 代碼如下:
var obj = "jeff wong";
alert(obj.constructor == String); //true
obj = new Cat();
alert(obj.constructor == Cat); //true
但是,下面的代碼您也可以測(cè)試一下:
復(fù)制代碼 代碼如下:
//alert(1.constructor); //數(shù)字常量 出錯(cuò) 數(shù)字常量無(wú)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沒(méi)有constructor屬性
none = undefined;
alert(obj.constructor); //undefined沒(méi)有constructor屬性
實(shí)驗(yàn)證明,數(shù)字型常量,null和undefined都沒(méi)有constructor屬性。
到這里,您會(huì)和樓豬一樣慶幸認(rèn)為終于大功告成了嗎?下面的代碼或許還能有點(diǎn)啟發(fā)和挖掘作用:
復(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 理解
原來(lái)對(duì)于原型鏈繼承的情況,constuctor也不那么好使了。那怎么辦?
3、直觀的instanceof
嘿嘿,有請(qǐng)instanceof隆重登場(chǎng)。看它的命名,好像是獲取某一個(gè)對(duì)象的實(shí)例,也不知這樣理解對(duì)不對(duì)?不管怎樣,我們還是動(dòng)手改進(jìn)上面的代碼測(cè)試一下先:
復(fù)制代碼 代碼如下:
function Animal() {
}
function Cat() {
}
Cat.prototype = new Animal();
Cat.prototype.CatchMouse = function () {
//do some thing
}
var garfield = new Cat();
alert(garfield instanceof Cat); //true 毫無(wú)疑問(wèn)
alert(garfield instanceof Animal); //true 可以理解
好了,關(guān)于Javascript的數(shù)據(jù)類型檢測(cè),樓豬就大致總結(jié)介紹到這里了。希望有心的高人補(bǔ)充。
JavaScript技術(shù):淺談javascript的數(shù)據(jù)類型檢測(cè),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。