JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹
有五種數(shù)據(jù)判斷類型方法typeof 、instanceof、constructor、Object.prototype.toString.call()、jquery.type()
一、typeof方法
typeof是個(gè)操作符,可以判斷基本數(shù)據(jù)類型(返回的結(jié)果只能是number,string,boolean,null,symbol,function,object)
返回值分以下幾種
對于基本類型。除了null值返回object以外,其他均返回正確的結(jié)果
對于引用值來說,除了function返回function類型,其他都返回object類型
例:
console.log(
typeof 100, //"number"
typeof 'abc', //"string"
typeof false, //"boolean"
typeof undefined, //"undefined"
typeof null, //"object"
typeof [1,2,3], //"object"
typeof {a:1,b:2,c:3}, //"object"
typeof function(){console.log('aaa');}, //"function"
typeof new Date(), //"object"
typeof /^[a-zA-Z]{5,20}$/, //"object"
typeof new Error() //"object"
typeof new Number(100), //'object'
typeof new String('abc'),// 'string'
typeof new Boolean(true),//'boolean'
)
二、instanceof方法
一般用來檢測引用數(shù)據(jù)類型,表達(dá)式為:A instanceof B,判斷A是否是B的實(shí)例,如果 A 是 B 的實(shí)例,則返回 true,否則返回 false,由構(gòu)造類型判斷出數(shù)據(jù)類型
console.log(
100 instanceof Number, //false
'dsfsf' instanceof String, //false
false instanceof Boolean, //false
undefined instanceof Object, //false
null instanceof Object, //false
[1,2,3] instanceof Array, //true
{a:1,b:2,c:3} instanceof Object, //true
function(){console.log('aaa');} instanceof Function, //true
new Date() instanceof Date, //true
/^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
new Error() instanceof Error //true
)
//注意: instanceof 后面一定要是對象類型,大小寫不能寫錯(cuò),該方法試用一些條件選擇或分支
還需要注意null和undefined都返回了false,這是因?yàn)樗鼈兊念愋途褪亲约罕旧?,并不是Object創(chuàng)建出來它們,所以返回了false。
三、constructor方法
constructor是prototype對象上的屬性,指向構(gòu)造函數(shù),
var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();
function Person(){
}
var tom = new Person();
// undefined和null沒有constructor屬性
console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有結(jié)果均為true
注意:除了undefined和null之外,其他類型都可以通過constructor屬性來判斷類型
方法四:Object.prototype.toString 方法
用來檢測對象類型
var toString = Object.prototype.toString;
toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"
toString是Object原型對象上的一個(gè)方法,該方法默認(rèn)返回其調(diào)用者的具體類型更嚴(yán)格的講,是 toString運(yùn)行時(shí)this指向的對象類型, 返回的類型格式為[object,xxx],xxx是具體的數(shù)據(jù)類型,其中包括:String,Number,Boolean,Undefined,Null,F(xiàn)unction,Date,Array,RegExp,Error,HTMLDocument等等都可以通過這個(gè)方法獲取到
5、無敵萬能的方法:jquery.type()
如果對象是undefined或null,則返回相應(yīng)的“undefined”或“null”。
jQuery.type( undefined ) === "undefined" jQuery.type() === "undefined" jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null"
如果對象有一個(gè)內(nèi)部的[[Class]]和一個(gè)瀏覽器的內(nèi)置對象的 [[Class]] 相同,我們返回相應(yīng)的 [[Class]] 名字
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
其他一切都將返回它的類型“object”。
6 . 自己也可以封裝一個(gè)獲取變量準(zhǔn)確類型的函數(shù)
function gettype(obj) {
var type = typeof obj;
if (type !== 'object') {
return type;
}
//如果不是object類型的數(shù)據(jù),直接用typeof就能判斷出來
//如果是object類型數(shù)據(jù),準(zhǔn)確判斷類型必須使用Object.prototype.toString.call(obj)的方式才能判斷
return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}
總結(jié)
到此這篇關(guān)于JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹的文章就介紹到這了,更多相關(guān)js 判斷數(shù)據(jù)類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JS數(shù)據(jù)類型分類及常用判斷方法
- JavaScript 判斷數(shù)據(jù)類型的4種方法
- 詳解JavaScript數(shù)據(jù)類型和判斷方法
- JS數(shù)據(jù)類型判斷的幾種常用方法
- JavaScript如何判斷input數(shù)據(jù)類型
- JS精確判斷數(shù)據(jù)類型代碼實(shí)例
- js的各種數(shù)據(jù)類型判斷的介紹
- js 判斷數(shù)據(jù)類型的幾種方法
- js 判斷各種數(shù)據(jù)類型的簡單方法(推薦)
- JavaScript中判斷數(shù)據(jù)類型的方法總結(jié)
- js 數(shù)據(jù)類型判斷的方法
相關(guān)文章
layui表格 返回的數(shù)據(jù)狀態(tài)異常的解決方法
今天小編就為大家分享一篇layui表格 返回的數(shù)據(jù)狀態(tài)異常的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript 翻頁測試頁(動(dòng)態(tài)創(chuàng)建標(biāo)簽并自動(dòng)翻頁)
javascript 翻頁測試頁(動(dòng)態(tài)創(chuàng)建標(biāo)簽并自動(dòng)翻頁),需要的朋友可以參考下。2009-12-12
JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過顯示下拉框
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過顯示下拉框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
uniapp video播放視頻 懸浮在屏幕無法滑動(dòng)
在uniapp中,需要使用<video></video>標(biāo)簽進(jìn)行播放動(dòng)態(tài)src的視頻,這里只是簡單的在App端播放視頻,且動(dòng)態(tài)賦值src,如果還有其它復(fù)雜的布局內(nèi)部嵌套video標(biāo)簽也是不成功的,例如:<swiper>、<scroll-view>等,感興趣的朋友跟隨小編一起看看吧2024-08-08
基于pako.js實(shí)現(xiàn)gzip的壓縮和解壓功能示例
這篇文章主要介紹了基于pako.js實(shí)現(xiàn)gzip的壓縮和解壓功能,結(jié)合具體實(shí)例形式分析了pako.js實(shí)現(xiàn)字符串壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

