亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹

瀏覽:49日期:2023-06-17 11:44:21

有五種數(shù)據(jù)判斷類型方法typeof 、instanceof、constructor、Object.prototype.toString.call()、jquery.type()

一、typeof方法

typeof是個操作符,可以判斷基本數(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ù)類型,表達式為:A instanceof B,判斷A是否是B的實例,如果 A 是 B 的實例,則返回 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 后面一定要是對象類型,大小寫不能寫錯,該方法試用一些條件選擇或分支

還需要注意null和undefined都返回了false,這是因為它們的類型就是自己本身,并不是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原型對象上的一個方法,該方法默認返回其調(diào)用者的具體類型更嚴(yán)格的講,是 toString運行時this指向的對象類型, 返回的類型格式為[object,xxx],xxx是具體的數(shù)據(jù)類型,其中包括:String,Number,Boolean,Undefined,Null,F(xiàn)unction,Date,Array,RegExp,Error,HTMLDocument等等都可以通過這個方法獲取到

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'

如果對象有一個內(nèi)部的[[Class]]和一個瀏覽器的內(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.9jQuery.type( /test/ ) === 'regexp'

其他一切都將返回它的類型“object”。6 . 自己也可以封裝一個獲取變量準(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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 99热在线免费观看 | 成人污网站 | 日本 亚洲 欧美 | 欧美视频在线观看网站 | 女人洗澡一级特黄毛片 | 亚洲午夜日韩高清一区 | 韩日在线播放 | 高清国产亚洲va精品 | 美女久久精品 | 日韩在线不卡视频 | 成人午夜电影免费完整在线看 | 99www综合久久爱com | 精品国产一区二区三区四 | 青青青久在线视频免费观看 | 国产一精品一aⅴ一免费 | bt7086 福利二区 最新合集 | 西川结衣在线精品视频 | 久久97精品久久久久久久看片 | 国产精品视频大全 | 欧美一区二区在线视频 | 日本韩国欧美在线观看 | 国产精品久久视频 | 国产情侣自拍偷拍 | 色婷婷久久综合中文久久一本 | 国产酒店视频 | 毛片毛片毛片毛片毛片毛片毛片 | 国产成人免费午夜性视频 | 精彩视频一区二区 | 亚洲 欧美 自拍 卡通 综合 | 色婷婷狠狠久久综合五月 | 亚洲精品久久久久综合网 | 欧美性色福利视频在线观看 | 国产精品成人免费观看 | 欧美黄网站免费观看 | 2022国产情侣真实露脸在线 | 国产亚洲精品观看91在线 | 亚洲色在线视频 | 国产成人在线播放视频 | 一本久久精品一区二区 | 99久久精品国产一区二区 | 国产精品一国产精品 |