JS檢測數據類型

JS中的數據類型

基本數據類型:Undefined、Null、Boolean、Number、String
複雜數據類型:Object
引用類型:Object、Array、Date、RegExp、Function
ES6新增數據類型:Symbol

let b = true;
let i = 3;
let s = 'aaa';
let u= undefined;
let n = null;
let a = ['a', 'b', 'c'];
let o = {a: 'a', b: 'b'};
let d = new Date();
let e = /at/g;
let f = function(){};
let m = Symbol();

typeof

typeof操作符是檢測基本數據類型的最佳工具,而檢測引用類型時統一返回object

console.log(typeof b); //boolean
console.log(typeof i); //number
console.log(typeof s); //string
console.log(typeof u); //undefined
console.log(typeof n); //object
console.log(typeof a); //object
console.log(typeof o); //object
console.log(typeof d); //object
console.log(typeof e); //object
console.log(typeof f); //function
console.log(typeof m); //symbol

typeof null 返回’object’,因爲特殊值null被認爲是一個空的對象引用。
Safari 5 及之前版本、Chrome 7及之前版本對正則表達式調用typeof操作符時會返回‘function’,而其他瀏覽器在這種情況下會返回‘object’

instanceof

typeof是檢測基本類型的有力工具,但檢測引用類型時,這個操作符用處不大,於是instanceof派上用場
如果變量是給定引用類型的實例,那instanceof操作符會返回true

console.log(b instanceof Boolean); // false
console.log(i instanceof Number); // false
console.log(s instanceof String); // false
console.log(u instanceof Object); // false
console.log(n instanceof Object); // false
console.log(a instanceof Array); // true
console.log(o instanceof Object); // true
console.log(d instanceof Date); //true
console.log(e instanceof RegExp); //true
console.log(f instanceof Function); // true
console.log(s instanceof Symbol); // false

根據規定,所有引用類型的值都是Object的實例。因此,在檢測一個引用類型值和Object構造函數時,instanceof 操作符始終會返回true。如果使用instanceof操作符檢測基本類型的值,則該操作符始終會返回false,因爲基本類型不是對象。

constructor

constructor 可以檢測類型,但由於構造函數指向可以變改變,這個方法並不可靠

console.log(b.constructor === Boolean); // true
console.log(i.constructor === Number); // true
console.log(s.constructor === String); // true
console.log(a.constructor === Array); // true
console.log(o.constructor === Object); // true
console.log(d.constructor === Date); //true
console.log(e.constructor === RegExp); //true
console.log(f.constructor === Function); // true
console.log(m.constructor === Symbol); //true

Object.prototype.toString.call

安全的類型檢測方法

console.log(Object.prototype.toString.call(b)); //[object Boolean]
console.log(Object.prototype.toString.call(i)); //[object Number]
console.log(Object.prototype.toString.call(s)); //[object String]
console.log(Object.prototype.toString.call(a)); //[object Array]
console.log(Object.prototype.toString.call(o)); //[object Object]
console.log(Object.prototype.toString.call(d)); //[object Date]
console.log(Object.prototype.toString.call(e)); //[object RegExp]
console.log(Object.prototype.toString.call(f)); //[object Function]
console.log(Object.prototype.toString.call(m)); //[object Symbol]

Array.isArray()

支持Array.isArray()方法的瀏覽器有IE9+、Firefox 4+、Opera 10.5+和Chrome

封裝的類型檢測方法

function getType(obj){
  let type  = typeof obj;
  if(type != "object"){
    return type;
  }
  return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章