function judgeType(change) {
if (arguments.length == 0) {
return '0';//無參數(shù)傳入
}
if (change === null) {
return 'null'
}
if (change === undefined && arguments.length > 0) {
return 'undefined'
}
if (change instanceof Function) {
return 'function'
}
if (change instanceof Array) {
return 'arry'
}
if (change instanceof Number || typeof change == 'number') {
return 'number'
}
if (change instanceof String || typeof change == 'string') {
return 'string'
}
if (change instanceof Boolean || typeof change == 'boolean') {
return 'boolean'
}
}ps:下面看下js 判斷各種數(shù)據(jù)類型

創(chuàng)新互聯(lián)專注于豐潤(rùn)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供豐潤(rùn)營(yíng)銷型網(wǎng)站建設(shè),豐潤(rùn)網(wǎng)站制作、豐潤(rùn)網(wǎng)頁設(shè)計(jì)、豐潤(rùn)網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務(wù),打造豐潤(rùn)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供豐潤(rùn)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
了解js的都知道, 有個(gè)typeof 用來判斷各種數(shù)據(jù)類型,有兩種寫法:typeof xxx ,typeof(xxx)
如下實(shí)例:
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
看到這里你肯定會(huì)問了:我怎么去區(qū)分對(duì)象,數(shù)組和null呢?
接下來我們就用到另外一個(gè)利器:Object.prototype.toString.call
這是對(duì)象的一個(gè)原生原型擴(kuò)展函數(shù),用來更精確的區(qū)分?jǐn)?shù)據(jù)類型。
我們來試試這個(gè)玩兒意兒:
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]看到這里,剛才的問題我們解決了。
constructor也能判斷數(shù)據(jù)類型:
如:
''.constructor==String
[].constructor==Array
var obj= new Object()
obj.constructor==Object 其實(shí)js 里面還有好多類型判斷 [object HTMLDivElement] div 對(duì)象 , [object HTMLBodyElement] body 對(duì)象 ,[object Document](IE)或者 [object HTMLDocument](firefox,google) ......各種dom節(jié)點(diǎn)的判斷,這些東西在我們寫插件的時(shí)候都會(huì)用到。
可以封裝的方法如下 :
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];
}
........
}這個(gè)獲取類型的方法有個(gè)簡(jiǎn)單的寫法:
var Type = (function() {
var type = {};
var typeArr = ['String', 'Object', 'Number', 'Array','Undefined', 'Function', 'Null', 'Symbol'];
for (var i = 0; i < typeArr.length; i++) {
(function(name) {
type['Is' + name] = function(obj) {
return Object.prototype.toString.call(obj) == '[object ' + name + ']';
}
})(typeArr[i]);
}
return type;
})(); 調(diào)用方法:Type.IsFunction(function() {}) Type.IsObject({})。。。。。
Type.Is.....
總結(jié)
以上所述是小編給大家介紹的判斷js數(shù)據(jù)類型的函數(shù)實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
當(dāng)前標(biāo)題:判斷js數(shù)據(jù)類型的函數(shù)實(shí)例詳解
文章URL:http://chinadenli.net/article30/ipgjso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作、域名注冊(cè)、標(biāo)簽優(yōu)化、商城網(wǎng)站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)