美文网首页
js判断数据类型

js判断数据类型

作者: _undefined | 来源:发表于2019-09-27 10:11 被阅读0次

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]
    })
}

相关文章

网友评论

      本文标题:js判断数据类型

      本文链接:https://www.haomeiwen.com/subject/stmouctx.html