js的各種數(shù)據(jù)類型判斷的介紹
1.typeof
typeof 用來判斷各種數(shù)據(jù)類型,有兩種寫法:typeof xxx , typeof(xxx)
例如:
typeof 2 輸出 number
typeof null 輸出 object
typeof {} 輸出 object
typeof [] 輸出 object
typeof (function(){}) 輸出 function
typeof undefined 輸出 undefined
typeof '222' 輸出 string
typeof true 輸出 boolean
這里面包含了js里面的五種數(shù)據(jù)類型 number、string、boolean、 undefined、object 和函數(shù)類型 function
2. instanceof
判斷已知對象類型的方法.instanceof 后面一定要是對象類型,并且大小寫不能錯,該方法適合一些條件選擇或分支。
var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
var f = function(){this.name="22";};
console.log(c instanceof Array) //true
console.log(d instanceof Date) //true
console.log(e instanceof Function) //true
// console.log(f instanceof function ) //false
3.constructor
根據(jù)對象的constructor判斷,返回對創(chuàng)建此對象的數(shù)組函數(shù)的引用。
var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
//注意: constructor 在類繼承時會出錯
4.prototype
所有數(shù)據(jù)類型均可判斷:Object.prototype.toString.call
這是對象的一個原生原型擴(kuò)展函數(shù),用來更精確的區(qū)分?jǐn)?shù)據(jù)類型。
var gettype=Object.prototype.toString
gettype.call('aaaa') 輸出 [object String]
gettype.call(2222) 輸出 [object Number]
gettype.call(true) 輸出 [object Boolean]
gettype.call(undefined) 輸出 [object Undefined]
gettype.call(null) 輸出 [object Null]
gettype.call({}) 輸出 [object Object]
gettype.call([]) 輸出 [object Array]
gettype.call(function(){}) 輸出 [object Function]
其實(shí)js 里面還有好多類型判斷 [object HTMLDivElement] div 對象 , [object HTMLBodyElement] body 對象 ,[object Document](IE)或者 [object HTMLDocument](firefox,google) ……各種dom節(jié)點(diǎn)的判斷,這些東西在我們寫插件的時候都會用到。
可以封裝的方法如下:
var gettype=Object.prototype.toString
var utility={
isObj:function(o){
return gettype.call(o)=="[object Object]";
},
isArray:function(o){
return gettype.call(o)=="[object Array]";
},
isNULL:function(o){
return gettype.call(o)=="[object Null]";
},
isDocument:function(){
return gettype.call(o)=="[object Document]"|| [object HTMLDocument];
}
........
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
bootstrap fileinput 插件使用項(xiàng)目總結(jié)(經(jīng)驗(yàn))
這篇文章主要介紹了bootstrap fileinput 插件使用項(xiàng)目總結(jié),是小編日常碰到的問題及解決方法,需要的朋友可以參考下2017-02-02
基于BootStrap Metronic開發(fā)框架經(jīng)驗(yàn)小結(jié)【一】框架總覽及菜單模塊的處理
這篇文章主要介紹了基于BootStrap Metronic開發(fā)框架經(jīng)驗(yàn)小結(jié)【一】框架總覽及菜單模塊的處理的相關(guān)資料,小編認(rèn)為非常具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
使用layui日期控件laydate對開始和結(jié)束時間進(jìn)行聯(lián)動控制的方法
今天小編就為大家分享一篇使用layui日期控件laydate對開始和結(jié)束時間進(jìn)行聯(lián)動控制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript使用正則表達(dá)式實(shí)現(xiàn)去掉空格之后的字符
這篇文章主要介紹了javascript使用正則表達(dá)式實(shí)現(xiàn)去掉空格之后的字符的方法,需要的朋友可以參考下2015-02-02
原生js實(shí)現(xiàn)fadein 和 fadeout淡入淡出效果
這篇文章主要介紹了通過原生js實(shí)現(xiàn)fadein 和 fadeout淡入淡出效果,需要的朋友可以參考下2014-06-06
用JavaScript 判斷用戶使用的是 IE6 還是 IE7
判斷IE瀏覽器的腳本,方便根據(jù)瀏覽器不懂,支持不同的代碼的分別調(diào)用。2008-01-01

