判斷js數(shù)據(jù)類型的函數(shù)實例詳解
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ù)類型
了解js的都知道, 有個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
看到這里你肯定會問了:我怎么去區(qū)分對象,數(shù)組和null呢?
接下來我們就用到另外一個利器:Object.prototype.toString.call
這是對象的一個原生原型擴展函數(shù),用來更精確的區(qū)分數(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]
看到這里,剛才的問題我們解決了。
constructor也能判斷數(shù)據(jù)類型:
如:
''.constructor==String
[].constructor==Array
var obj= new Object()
obj.constructor==Object
其實js 里面還有好多類型判斷 [object HTMLDivElement] div 對象 , [object HTMLBodyElement] body 對象 ,[object Document](IE)或者 [object HTMLDocument](firefox,google) ......各種dom節(jié)點的判斷,這些東西在我們寫插件的時候都會用到。
可以封裝的方法如下 :
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];
}
........
}
這個獲取類型的方法有個簡單的寫法:
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ù)實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關(guān)文章
js調(diào)用webservice中的方法實現(xiàn)思路及代碼
js調(diào)用webservice還算是一個比較實用的功能,本文提供了實現(xiàn)思路及代碼,感興趣的你可不要錯過了哈,希望本文可以幫助到你啊2013-02-02
淺談javascript運算符——條件,逗號,賦值,()和void運算符
下面小編就為大家?guī)硪黄獪\談javascript運算符——條件,逗號,賦值,()和void運算符。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07
基于element?UI?input組件自行封裝“數(shù)字區(qū)間”輸入框組件的問題及解決
這篇文章主要介紹了基于element?UI?input組件自行封裝“數(shù)字區(qū)間”輸入框組件,實現(xiàn)代碼可以分為兩塊一塊為組件的封裝代碼,一塊為文中實現(xiàn)效果的演示代碼,對element?UI數(shù)字區(qū)間輸入組件相關(guān)知識感興趣的朋友一起看看吧2022-05-05

