JS 如何判斷數(shù)據(jù)類型
在 JS 中有許多中提供了幾個(gè)原生的判斷數(shù)據(jù)類型方法,本文主要介紹 JS 中的判斷基本數(shù)據(jù)類型的四個(gè)方法:
typeof, instanceof, constructor, prototype
typeof
相信用 JS 的都肯定用過 typeof
來判斷一些基本類型尔觉,但可能很多人都會(huì)誤以為 typeof
是一個(gè)函數(shù)昭卓。
實(shí)際上 typeof
并不是一個(gè)函數(shù)骤星,也不是一個(gè)語句流强,而是一個(gè)一元運(yùn)算符(就像 ++, –, !)脑慧。
因?yàn)橐辉\(yùn)算符是可以用括號(hào)將運(yùn)算數(shù)包裹起來的抡驼,所以讓看起來非常想一個(gè)函數(shù),如 !(condition)
接下來回到正題晰奖,用 typeof
可以判斷哪幾種基本的數(shù)據(jù)類型呢
使用 typeof
判斷后返回的是 字符串 類型
- string 如果這個(gè)值是字符串
- number 如果這個(gè)值是數(shù)值
- boolean 如果這個(gè)值是布爾值
- function 如果這個(gè)值是函數(shù)
- object 如果這個(gè)值是對(duì)象谈撒、數(shù)組或 null
- undefined 如果這個(gè)值未定義
可以看到 typeof
并不能單獨(dú)的判斷出數(shù)組,而是將其判斷成對(duì)象匾南。當(dāng)然你可以使用 Array.isArray(ob)
來繼續(xù)對(duì) Object 進(jìn)行數(shù)組的判斷
console.log(typeof 1 === 'number') // => true
console.log(typeof 'wjh' === 'string') // => true
console.log(typeof false === 'boolean') // => true
console.log(typeof [] === 'object') // => true
console.log(typeof {} === 'object') // => true
console.log(typeof null === 'object') // => true
console.log(typeof un === 'undefined') // => true
instanceof
instanceof
運(yùn)算符用來判斷一個(gè)構(gòu)造函數(shù)的 prototype 屬性所指向的對(duì)象是否存在另外一個(gè)要檢測對(duì)象的原型鏈上啃匿,需要區(qū)分大小寫。( instanceof 不是函數(shù)蛆楞,是一個(gè)雙目運(yùn)算符如 +, -… )
需要注意的是溯乒,instanceof 只能用來判斷對(duì)象和函數(shù),不能用來判斷字符串和數(shù)字等豹爹。判斷它是否為字符串和數(shù)字時(shí)裆悄,只會(huì)返回false。
var arr = []
var obj = {}
var func = functino () { console.log('Hello world') }
console.log(arr instanceof Array) // => true
console.log(obj instanceof Object) // => true
console.log(func instanceof Function) // => true
console.log(1 instanceof Number) // => false
console.log('str' instanceof String) // => false
constructor
在 Javascript 中每一個(gè)具有原型的對(duì)象都擁有一個(gè) constructor
屬性 (Object.constructor)
constructor
屬性返回對(duì)創(chuàng)建此對(duì)象的數(shù)組函數(shù)的引用臂聋。
constructor
可以判斷所有類型光稼,包括 Array
// String
var str = "字符串"
console.log(str.constructor) // function String() { [native code] }
console.log(str.constructor === String) // true
// Array
var arr = [1, 2, 3]
console.log(arr.constructor) // function Array() { [native code] }
console.log(arr.constructor === Array) // true
// Number
var num = 5;
console.log(num.constructor) // function Number() { [native code] }
console.log(num.constructor === Number) // true
// Object
var obj = {};
console.log(obj.constructor) // function Object() { [native code] }
console.log(obj.constructor === Object) // tru
但是或南, constructor在類繼承時(shí)會(huì)出錯(cuò)
function A(){};
function B(){};
var aObj = new A();
console.log(aObj.constructor === A); //true;
console.log(aObj.constructor === B); //false;
function C(){};
function D(){};
C.prototype = new D(); //C繼承自D
var cObj = new C();
console.log(cObj.constructor === C); //false;
console.log(cObj.constructor === D); //true;
prototype
以上三種方法多少都會(huì)有一些不能判斷的情況。為了保證兼容性艾君,可以通過 Object.prototype.toString
方法采够,判斷某個(gè)對(duì)象值屬于哪種內(nèi)置類型。
- 需要注意大小寫
console.log(Object.prototype.toString.call('字符串') === '[object String]') // -------> true;
console.log(Object.prototype.toString.call(123) === '[object Number]') // -------> true;
console.log(Object.prototype.toString.call([1,2,3]) === '[object Array]') // -------> true;
console.log(Object.prototype.toString.call(new Date()) === '[object Date]') // -------> true;
console.log(Object.prototype.toString.call(function a(){}) === '[object Function]') // -------> true;
console.log(Object.prototype.toString.call({}) === '[object Object]') // -------> true;