色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

淺談javascript的數(shù)據(jù)類型檢測

一、Javascript的數(shù)據(jù)
Javascript的數(shù)據(jù)分為兩種:簡單數(shù)據(jù)和復(fù)雜數(shù)據(jù)。簡單數(shù)據(jù)包含number,string,boolean,undefined和null這五種;復(fù)雜數(shù)據(jù)只有一種即object。【此處友情鳴謝李戰(zhàn)老師,<<悟透JavaScript>>寫得太傳神,印象太深刻了】

二、Javascript的數(shù)據(jù)類型檢測
1、萬能的typeof
我們先測試一下通過typeof來獲取簡單數(shù)據(jù)類型。什么也別說了,上代碼是王道:
復(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

正如你所看到的那樣,通過typeof運(yùn)算符,前面四個(gè)簡單數(shù)據(jù)類型完全在意料之中,但是typeof null卻返回object。應(yīng)該注意到,null是null類型的唯一值,但null并不是object,具有null值的變量也并非object,所以直接通過typeof,并不能正確得到null類型。 要正確獲取簡單數(shù)據(jù)類型,只要在getType的地方加點(diǎn)改進(jìn)就可以了:
復(fù)制代碼 代碼如下:
function getType(obj) {
return (obj === null) ? "null" : typeof (obj);
}

接著來試一下復(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(請注意大小寫)返回了function,不管是Javascript的常見內(nèi)置對象Object,String或者Date等等,還是自定義function,通過typeof返回的無一例外,通通都是object。但是對于自定義function,我們更愿意得到它的“廬山真面目”(示例中即Cat,而非object),而顯然,typeof不具備這種轉(zhuǎn)換處理能力。
2、constructor,想大聲說愛你
既然萬能的typeof也有無解的時(shí)候,那么我們怎么判斷一個(gè)變量是否是自定義的function實(shí)例呢?我們知道,Javascript的所有對象都有一個(gè)constructor屬性,這個(gè)屬性可以幫我們判斷object數(shù)據(jù)類型,尤其是對自定義function同樣適用:
復(fù)制代碼 代碼如下:
var obj = "jeff wong";
alert(obj.constructor == String); //true
obj = new Cat();
alert(obj.constructor == Cat); //true

但是,下面的代碼您也可以測試一下:
復(fù)制代碼 代碼如下:
//alert(1.constructor); //數(shù)字常量 出錯(cuò) 數(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í)驗(yàn)證明,數(shù)字型常量,null和undefined都沒有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 理解

原來對于原型鏈繼承的情況,constuctor也不那么好使了。那怎么辦?
3、直觀的instanceof
嘿嘿,有請instanceof隆重登場。看它的命名,好像是獲取某一個(gè)對象的實(shí)例,也不知這樣理解對不對?不管怎樣,我們還是動(dòng)手改進(jìn)上面的代碼測試一下先:
復(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 毫無疑問
alert(garfield instanceof Animal); //true 可以理解

好了,關(guān)于Javascript的數(shù)據(jù)類型檢測,樓豬就大致總結(jié)介紹到這里了。希望有心的高人補(bǔ)充。

JavaScript技術(shù)淺談javascript的數(shù)據(jù)類型檢測,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 大新县| 博爱县| 嵩明县| 镇远县| 陵川县| 黄山市| 新闻| 永福县| 涞水县| 江北区| 三台县| 揭阳市| 广丰县| 马尔康县| 富源县| 凌源市| 黑龙江省| 根河市| 大英县| 武宁县| 南木林县| 河南省| 沿河| 宿州市| 白朗县| 宁城县| 江山市| 浏阳市| 新竹市| 白沙| 阜平县| 宜兰县| 新龙县| 奈曼旗| 娱乐| 江阴市| 高雄县| 渭南市| 平陆县| 武安市| 驻马店市|