更新時(shí)間:2022-09-22 09:09:22 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1415次
大家都知道,Java中有八大基本數(shù)據(jù)類型,那么,判斷數(shù)據(jù)類型的方法有哪些?動(dòng)力節(jié)點(diǎn)小編來(lái)告訴大家。
只能用來(lái)判斷變量的原型鏈上是否有構(gòu)造函數(shù)的prototype屬性(兩個(gè)對(duì)象是否屬于原型鏈的關(guān)系),不一定能獲取對(duì)象的具體類型
Instanceof 不適用判斷原始類型的值,只能用于判斷對(duì)象是否從屬關(guān)系
原理:因?yàn)锳 instanceof B 可以判斷A是不是B的實(shí)例,返回一個(gè)布爾值,由構(gòu)造類型判斷出數(shù)據(jù)類型
分析:[].proto 的原型 是指向Array.prototype 的,說(shuō)明兩個(gè)對(duì)象是屬于同一條原型鏈的,返回true
console.log(arr instanceof Array ); // true
console.log(date instanceof Date ); // true
console.log(fn instanceof Function ); // true
//注意: instanceof 后面一定要是對(duì)象類型,大小寫不能寫錯(cuò),該方法試用一些條件選擇或分支
// 空對(duì)象的判斷問(wèn)題
let obj1 = {}
console.log(obj1 instanceof Object) // true
let obj2 = Object.create(null)
console.log(obj2 instanceof Object) // false
let obj3 = Object.create({})
console.log(obj3 instanceof Object) // true
適用于判斷除了null外的基礎(chǔ)類型和function
可以判斷數(shù)據(jù)類型,它返回表示數(shù)據(jù)類型的字符串(返回結(jié)果只能包括number,boolean,string,function,object,undefined)
可以使用typeof判斷變量是否存在(如if(typeof a!=“undefined”){…})
無(wú)法判斷對(duì)象和數(shù)組,兩者都返回object
typeof '5' // string
typeof 5 // number
typeof null // object 因?yàn)閚ull被認(rèn)為是空對(duì)象
typeof undefined // undefined
typeof true // boolean
typeof Symbol('5') // symbol
typeof 5n // bigint
typeof new Object(); // object
typeof new Function(); // function
或者
console.log(typeof([1,2])); //object
console.log(typeof(6)); //number
console.log(typeof(true)); //boolean
原理:每一個(gè)實(shí)例對(duì)象都可通過(guò)constructor來(lái)訪問(wèn)它的構(gòu)造函數(shù),其實(shí)也是根據(jù)原型鏈的原理來(lái)的。
構(gòu)造函數(shù)屬性判斷,返回一個(gè)布爾值。
由于undefined和null是無(wú)效的對(duì)象,因此是沒(méi)有constructor屬性的,這兩個(gè)值不能用這種方法判斷。
console.log(arr.constructor === Array); //true
//或
let a = [1,2]
console.log(a.constructor === Function); //false
'5'.__proto__.constructor === String // true
[5].__proto__.constructor === Array // true
undefined.__proto__.constructor // Cannot read property '__proto__' of undefined
null.__proto__.constructor // Cannot read property '__proto__' of undefined
Object.prototype.toString方法返回對(duì)象的類型字符串,因此可用來(lái)判斷一個(gè)值的類型。
因?yàn)閷?shí)例對(duì)象有可能會(huì)自定義toString方法,會(huì)覆蓋Object.prototype.toString,所以在使用時(shí),最好加上call。
所有的數(shù)據(jù)類型都可以使用此方法進(jìn)行檢測(cè),且非常精準(zhǔn)。
返回一個(gè)布爾值。
console.log(toString.call('123')); //[object String]
console.log(toString.call(undefined)); //[object Undefined]
console.log(toString.call({})); //[object Object]
console.log(toString.call([])); //[object Array]
Object.prototype.toString.call('5') // [object String]
Object.prototype.toString.call(5) // [object Number]
Object.prototype.toString.call([5]) // [object Array]
Object.prototype.toString.call(true) // [object Boolean]
Object.prototype.toString.call(undefined) // [object Undefined]
Object.prototype.toString.call(null) // [object Null]
Object.prototype.toString.call(new Function()); // [object Function]
Object.prototype.toString.call(new Date()); // [object Date]
Object.prototype.toString.call(new RegExp()); // [object RegExp]
Object.prototype.toString.call(new Error()); // [object Error]
ES5方法,返回一個(gè)布爾值。
var a = [1,2];
console.log(Array.isArray(a)); //true
jQuery提供了一系列工具方法,用來(lái)判斷數(shù)據(jù)類型,以彌補(bǔ)JavaScript的typeof運(yùn)算符的不足。以下方法對(duì)參數(shù)進(jìn)行判斷,返回一個(gè)布爾值。
jQuery.isArray() //是否為數(shù)組
jQuery.isEmptyObject() //是否為空對(duì)象 (不含可枚舉屬性)。
jQuery.isFunction() //是否為函數(shù)
jQuery.isNumberic() //是否為數(shù)字
jQuery.isPlainObject() //是否為使用“{}”或“new Object”生成對(duì)象,而不是瀏覽器原生提供的對(duì)象。
jQuery.isWindow() //是否為window對(duì)象;
jQuery.isXMLDoc() //判斷一個(gè)DOM節(jié)點(diǎn)是否處于XML文檔中。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743