JavaScript监测值类型最佳实现

1,字符串、数字、布尔值、undefined、function,最佳方法typeof

typeof "" === "string"

typeof 1 === "number"

typeof true === "boolean"

typeof undefined === "undefined"

2,null 最佳方法 ===

null === null

3,[],{} 最佳方法 Object.prototype.toString.call()

Object.prototype.toString.call([])

"[object Array]"

Object.prototype.toString.call({})

"[object Object]"

Object.prototype.toString.call(null)

"[object Null]"

Object.prototype.toString.call(1)

"[object Number]"

Object.prototype.toString.call("")

"[object String]"

Object.prototype.toString.call(true)

"[object Boolean]"

Object.prototype.toString.call(undefined)

"[object Undefined]"

Object.prototype.toString.call(new Date())

"[object Date]"

 Object.prototype.toString.call(function () {})

"[object Function]"

posted @ 2017-04-20 16:08  minimal虾米  阅读(73)  评论(0编辑  收藏  举报