判断数据类型

97 阅读1分钟

typeof

只能判断6种数据类型,string、number、boolean、undefined、symbol、function

typeof undefined // undefined
typeof null // object
typeof true // boolean
typeof 1    // number
typeof '1'   // string
typeof [1,2]  // object
typeof {}  // object
typeof new Date()  // object

Object.prototype.toString.call

const aaaa = [1,2,3]
const bbb = {name: 'zz'}

var toString = Object.prototype.toString

toString.call(bbb) // [object Object]
toString.call(aaa) // [object Array]
toString.call(new Date) // [object Date]
toString.call(new String) // [object String]
toString.call(Math) // [object Math]

toString.call(undefined) // [object Undefined]
toString.call(null) // [object Null]
toString.call('a') // [object String]