更新時間:2022-09-22 09:09:22 來源:動力節點 瀏覽1429次
大家都知道,Java中有八大基本數據類型,那么,判斷數據類型的方法有哪些?動力節點小編來告訴大家。
只能用來判斷變量的原型鏈上是否有構造函數的prototype屬性(兩個對象是否屬于原型鏈的關系),不一定能獲取對象的具體類型
Instanceof 不適用判斷原始類型的值,只能用于判斷對象是否從屬關系
原理:因為A instanceof B 可以判斷A是不是B的實例,返回一個布爾值,由構造類型判斷出數據類型
分析:[].proto 的原型 是指向Array.prototype 的,說明兩個對象是屬于同一條原型鏈的,返回true
console.log(arr instanceof Array ); // true
console.log(date instanceof Date ); // true
console.log(fn instanceof Function ); // true
//注意: instanceof 后面一定要是對象類型,大小寫不能寫錯,該方法試用一些條件選擇或分支
// 空對象的判斷問題
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外的基礎類型和function
可以判斷數據類型,它返回表示數據類型的字符串(返回結果只能包括number,boolean,string,function,object,undefined)
可以使用typeof判斷變量是否存在(如if(typeof a!=“undefined”){…})
無法判斷對象和數組,兩者都返回object
typeof '5' // string
typeof 5 // number
typeof null // object 因為null被認為是空對象
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
原理:每一個實例對象都可通過constructor來訪問它的構造函數,其實也是根據原型鏈的原理來的。
構造函數屬性判斷,返回一個布爾值。
由于undefined和null是無效的對象,因此是沒有constructor屬性的,這兩個值不能用這種方法判斷。
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方法返回對象的類型字符串,因此可用來判斷一個值的類型。
因為實例對象有可能會自定義toString方法,會覆蓋Object.prototype.toString,所以在使用時,最好加上call。
所有的數據類型都可以使用此方法進行檢測,且非常精準。
返回一個布爾值。
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方法,返回一個布爾值。
var a = [1,2];
console.log(Array.isArray(a)); //true
jQuery提供了一系列工具方法,用來判斷數據類型,以彌補JavaScript的typeof運算符的不足。以下方法對參數進行判斷,返回一個布爾值。
jQuery.isArray() //是否為數組
jQuery.isEmptyObject() //是否為空對象 (不含可枚舉屬性)。
jQuery.isFunction() //是否為函數
jQuery.isNumberic() //是否為數字
jQuery.isPlainObject() //是否為使用“{}”或“new Object”生成對象,而不是瀏覽器原生提供的對象。
jQuery.isWindow() //是否為window對象;
jQuery.isXMLDoc() //判斷一個DOM節點是否處于XML文檔中。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習