js判断数据类型

2019-09-27  本文已影响0人  _undefined

typeof

只能判断除了null的基础数据类型

typeof 'hi' // string
typeof 123 // number
typeof true // boolean
typeof Symbol() // symbol
typeof undefined // undefined
typeof null // object

symbol 是es6新增的基础数据类型

instanceof

({}) instanceof Object // true
([]) instanceof Array // true
(function () {}) instanceof Function true
(/hello/) instanceof RegExp // true
(new Date()) instanceof Date // true

Object.prototype.toString.call()

function dataType(data) {
    return Object.prototype.toString.call(data).replace(/\[object (\w*)\]/, function() {
        return arguments[1]
    })
}
上一篇 下一篇

猜你喜欢

热点阅读