1. 程式人生 > 其它 >js型別判斷大總結(typeof、instanceof、Object.prototype.toString.call())

js型別判斷大總結(typeof、instanceof、Object.prototype.toString.call())

首先來談談下,js現在有多少種資料型別;截至到現在js目前有10種資料型別,其中包括7種基本資料型別,和3種引用資料型別

基本資料型別:

  number(數值型別,其中NaN、Infinity、-Infinity都屬於number型別)

  string(字串型別)

  null

  undefined

  bollean(true和false)

  symbol(es6新型別)

  bigint(es6新型別)

引用資料型別:

  array(陣列型別)

  object(物件型別包括自定義物件和js內建物件等)

  function(函式型別)

那js有這麼多種資料型別,我們要怎麼判斷區分出他們嘞?

可以使用一下三種方法,讓我們來看看這三種方法有什麼不同吧!

判斷資料型別的方法:

  typeof

  instanceof

  Object.prototype.toString.call()

首先typeof判斷基本資料型別時除了null型別返回'object'外,其他基本資料型別都正常返回;typeof判斷引用資料型別時,除了function型別返回‘function’外,其他引用資料型別(包括js內建物件)都返回'object'--------- 總的來說,typeof基本可以區分基本資料型別,基本不能區分引用資料型別,但各有兩個例外,null返回'object'、function返回'function'

let type_1 = 1,type_2 = true,type_3 = null,type_4 = undefined,type_5 = '',
        type_6 = Symbol(''),type_7 = BigInt("9007199254740995"),
        type_8 = new Function(),type_9 = [],type_10 = {},type_11 = Infinity

        // 內建js物件
        let date = new Date(),map = new Map(),set = new Set()

        console.log(
'1-',typeof type_1) // 'number' console.log('2-',typeof type_2) // 'bollean' console.log('3-',typeof type_3) // 'object' 作為基本資料,null返回'object' console.log('4-',typeof type_4) // 'undefined' console.log('5-',typeof type_5) // 'string' console.log('6-',typeof type_6) // 'symbol' console.log('7-',typeof type_7) // 'bigint' console.log('8-',typeof type_8) // 'function' 作為引用資料型別,function返回'function' console.log('9-',typeof type_9) // 'object' console.log('10-',typeof type_10) // 'object' // 其他js內建物件都返回'object' console.log('date-',typeof date) // 'object' console.log('map-',typeof map) // 'object' console.log('set-',typeof set) // 'object'

 

竟然typeof區分不出null和除了function之外的引用資料型別,那我們要怎麼辦呢?這時候可以考慮一下instanceof了;instanceof 操作符可以用來判斷某個建構函式的 prototype 屬性是否存在另外一個要檢測物件的原型鏈上。 也就是判斷instanceof前面的物件是否是後面的類或物件的例項。所以我們可以通過instanceof來區分引用資料型別。

let type_1 = 1,type_2 = true,type_3 = null,type_4 = undefined,type_5 = '',
        type_6 = Symbol(''),type_7 = BigInt("9007199254740995"),
        type_8 = new Function(),type_9 = [],type_10 = {}

        // 內建js物件
        let date = new Date(),map = new Map(),set = new Set()

        console.log(type_8 instanceof Function)  // true
        console.log(type_9 instanceof Array)     // true
        console.log(type_10 instanceof Object)   // true
        console.log(date instanceof Date)        // true
        console.log(map instanceof Map)          // true
        console.log(set instanceof Set)          // true
        // object為所有引用資料型別的原型(處在原型鏈頂端)
        console.log(type_8 instanceof Object&&type_9 instanceof Object&&date instanceof Object)  // true

好了現在就剩下孤零零的null不能被區分了,而且上面的兩種方法都不能完全滿足我們的需要,這時候就要出動萬精油老大哥了Object.prototype.toString.call(),關於其中的原理可以參考這位大佬的部落格

從深入到通俗:Object.prototype.toString.call() - 知乎 (zhihu.com)

let type_1 = 1,type_2 = true,type_3 = null,type_4 = undefined,type_5 = '',
        type_6 = Symbol(''),type_7 = BigInt("9007199254740995"),
        type_8 = new Function(),type_9 = [],type_10 = {}

        // 內建js物件
        let date = new Date(),map = new Map(),set = new Set()

        console.log(Object.prototype.toString.call(type_1))   // '[object Number]'
        console.log(Object.prototype.toString.call(type_2))   // '[object Boolean]'
        console.log(Object.prototype.toString.call(type_3))   // '[object Null]'
        console.log(Object.prototype.toString.call(type_4))   // '[object Undefined]'
        console.log(Object.prototype.toString.call(type_5))   // '[object String]'
        console.log(Object.prototype.toString.call(type_6))   // '[object Symbol]'
        console.log(Object.prototype.toString.call(type_7))   // '[object BigInt]'
        console.log(Object.prototype.toString.call(type_8))   // '[object Function]'
        console.log(Object.prototype.toString.call(type_9))   // '[object Array]'
        console.log(Object.prototype.toString.call(type_10))  // '[object Object]'
        console.log(Object.prototype.toString.call(date))     // '[object Date]'
        console.log(Object.prototype.toString.call(map))      // '[object Map]'
        console.log(Object.prototype.toString.call(set))      // '[object Set]'

這樣看來,Object.prototype.toString.call()還真是yyds!