判斷js數(shù)據(jù)類(lèi)型的函數(shù)實(shí)例詳解
function judgeType(change) { if (arguments.length == 0) { return '0';//無(wú)參數(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ù)類(lèi)型
了解js的都知道, 有個(gè)typeof 用來(lái)判斷各種數(shù)據(jù)類(lèi)型,有兩種寫(xiě)法: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ù)類(lèi)型 number string boolean undefined object和函數(shù)類(lèi)型 function
看到這里你肯定會(huì)問(wèn)了:我怎么去區(qū)分對(duì)象,數(shù)組和null呢?
接下來(lái)我們就用到另外一個(gè)利器:Object.prototype.toString.call
這是對(duì)象的一個(gè)原生原型擴(kuò)展函數(shù),用來(lái)更精確的區(qū)分?jǐn)?shù)據(jù)類(lèi)型。
我們來(lái)試試這個(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]
看到這里,剛才的問(wèn)題我們解決了。
constructor也能判斷數(shù)據(jù)類(lèi)型:
如:
''.constructor==String [].constructor==Array var obj= new Object() obj.constructor==Object
其實(shí)js 里面還有好多類(lèi)型判斷 [object HTMLDivElement]
div 對(duì)象 , [object HTMLBodyElement] body 對(duì)象 ,[object Document](IE)
或者 [object HTMLDocument](firefox,google)
......各種dom節(jié)點(diǎn)的判斷,這些東西在我們寫(xiě)插件的時(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è)獲取類(lèi)型的方法有個(gè)簡(jiǎn)單的寫(xiě)法:
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ù)類(lèi)型的函數(shù)實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
- js 判斷數(shù)據(jù)類(lèi)型的幾種方法
- js 判斷各種數(shù)據(jù)類(lèi)型的簡(jiǎn)單方法(推薦)
- Javascript如何判斷數(shù)據(jù)類(lèi)型和數(shù)組類(lèi)型
- js的各種數(shù)據(jù)類(lèi)型判斷的介紹
- JavaScript中判斷數(shù)據(jù)類(lèi)型的方法總結(jié)
- 淺談js數(shù)據(jù)類(lèi)型判斷與數(shù)組判斷
- JS數(shù)據(jù)類(lèi)型判斷的幾種常用方法
- JavaScript如何判斷input數(shù)據(jù)類(lèi)型
- JS中數(shù)據(jù)類(lèi)型的正確判斷方法實(shí)例
相關(guān)文章
js調(diào)用webservice中的方法實(shí)現(xiàn)思路及代碼
js調(diào)用webservice還算是一個(gè)比較實(shí)用的功能,本文提供了實(shí)現(xiàn)思路及代碼,感興趣的你可不要錯(cuò)過(guò)了哈,希望本文可以幫助到你啊2013-02-02Ajax跨域?qū)崿F(xiàn)代碼(后臺(tái)jsp)
這篇文章主要介紹了Ajax跨域?qū)崿F(xiàn)代碼(后臺(tái)jsp),需要的朋友可以參考下2017-01-01JS對(duì)img進(jìn)行操作(換圖片/切圖/輪換/停止)
JS對(duì)img進(jìn)行操作包括:換圖片/切圖/輪換/停止輪換等等,在本文將逐一實(shí)現(xiàn),感興趣的朋友可以參考下哈,希望對(duì)你學(xué)習(xí)js知識(shí)有所幫助2013-04-04淺談javascript運(yùn)算符——條件,逗號(hào),賦值,()和void運(yùn)算符
下面小編就為大家?guī)?lái)一篇淺談javascript運(yùn)算符——條件,逗號(hào),賦值,()和void運(yùn)算符。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07基于element?UI?input組件自行封裝“數(shù)字區(qū)間”輸入框組件的問(wèn)題及解決
這篇文章主要介紹了基于element?UI?input組件自行封裝“數(shù)字區(qū)間”輸入框組件,實(shí)現(xiàn)代碼可以分為兩塊一塊為組件的封裝代碼,一塊為文中實(shí)現(xiàn)效果的演示代碼,對(duì)element?UI數(shù)字區(qū)間輸入組件相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05jquery單行文字向上滾動(dòng)效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了jquery單行文字向上滾動(dòng)效果的具體實(shí)現(xiàn),此效果適應(yīng)于很多場(chǎng)景,會(huì)的不會(huì)的都要學(xué)習(xí)下啊2014-09-09js中switch case循環(huán)實(shí)例代碼
這篇文章主要介紹了js中switch case循環(huán)實(shí)例代碼,有需要的朋友可以參考一下2013-12-12