四種Javascript類型檢測(cè)的方式
一、typeof
主要用于判斷基本數(shù)據(jù)類型 。使用方式:typeof(表達(dá)式)和typeof 變量名,第一種是對(duì)表達(dá)式做運(yùn)算,第二種是對(duì)變量做運(yùn)算。 typeof運(yùn)算符的返回類型為字符串,值包括如下幾種:
- 'undefined':未定義的變量或值
- 'boolean':布爾類型的變量或值
- 'string' :字符串類型的變量或值
- 'number':數(shù)字類型的變量或值
- 'object' :對(duì)象類型的變量或值,或者null(這個(gè)是js歷史遺留問(wèn)題,將null作為object類型處理)
- 'function' :函數(shù)類型的變量或值
示例如下:
console.log(typeof a); //'undefined'
console.log(typeof(true)); //'boolean'
console.log(typeof '123'); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof null); //'object'
var obj = new String(); console.log(typeof(obj)); //'object'
var fn = function(){}; console.log(typeof(fn)); //'function'
console.log(typeof(class c{})); //'function'
typeof的不足之處:
- 不能區(qū)分對(duì)象、數(shù)組、正則,對(duì)它們操作都返回"object";(正則特殊一點(diǎn)后面說(shuō))
- Safar5,Chrome7之前的版本對(duì)正則對(duì)象返回 'function'
- 在IE6,7和8中,大多數(shù)的宿主對(duì)象是對(duì)象,而不是函數(shù);如:typeof alert; //object
- 而在非ID瀏覽器或則IE9以上(包含IE9),typeof alert; //function
- 對(duì)于null,返回的是object.
總結(jié):
typeof運(yùn)算符用于判斷對(duì)象的類型,但是對(duì)于一些創(chuàng)建的對(duì)象,它們都會(huì)返回'object',有時(shí)我們需要判斷該實(shí)例是否為某個(gè)對(duì)象的實(shí)例,那么這個(gè)時(shí)候需要用到instanceof運(yùn)算符。
二、instanceof
用于引用數(shù)據(jù)類型的判斷。所有引用數(shù)據(jù)類型的值都是Object的實(shí)例。目的是判斷一個(gè)對(duì)象在其原型鏈上是否存在構(gòu)造函數(shù)的prototype屬性。 用法:
variable instanceof constructor
示例如下:
// example
var arr = [];
由于:
1. arr.constructor === Array
2. arr.__proto__ === Array.prototype
3. arr.__poto__.proto__ === Object.prototype
所以, 以下都返回true
1. arr instanceof arr.constructor(Array)
2. arr instanceof arr.__proto__.constructor(Array)
3. arr instanceof arr.__proto__.__poto__.constructor(Object)
如果你了解原型鏈的話,你很快就會(huì)得出一些結(jié)論:
1. 所有對(duì)象 instanceof Object 都會(huì)返回 true
2. 所有函數(shù) instanceof Function 都會(huì)返回 true
總結(jié):
instanceof不僅能檢測(cè)構(gòu)造對(duì)象的構(gòu)造器,還檢測(cè)原型鏈。instanceof要求前面是個(gè)對(duì)象,后面是一個(gè)構(gòu)造函數(shù)。而且返回的是布爾型的,不是true就是false。
3、Array.isArray()
Array.isArray()可以用于判斷數(shù)組類型,支持的瀏覽器有IE9+、FireFox 4+、Safari 5+、Chrome; 兼容實(shí)現(xiàn):
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
示例如下:
// 1.
Array.isArray([1, 2, 3, 4]); // --> true
// 2.
var obj = {
a: 1,
b: 2
};
Array.isArray(obj); // --> false
// 3.
Array.isArray(new Array); // --> true
//4.
Array.isArray("Array"); // --> false
總結(jié):
isArray是一個(gè)靜態(tài)方法,使用Array對(duì)象(類)調(diào)用,而不是數(shù)組對(duì)象實(shí)例。其中Array.prototype 也是一個(gè)數(shù)組,Array.isArray 優(yōu)于 instanceof。
四、Object.prototype.toString.call()
判斷某個(gè)對(duì)象值屬于哪種內(nèi)置類型, 最靠譜的做法就是通過(guò)Object.prototype.toString方法。object.prototype.toString()輸出的格式就是[object 對(duì)象數(shù)據(jù)類型]。
示例如下:
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]