js检查数据类型的方式

99 阅读2分钟

一、typeof

typeof的返回值都是字符串,表示该值的类型。返回值类型:

  1. number
  2. bigint
  3. string
  4. boolean
  5. undefined
  6. symbol
  7. function
  8. object

1、typeof返回值为number

      console.log(typeof 1)
      console.log(typeof NaN)
      console.log(typeof Number(1))
      console.log(typeof Number('abc')) // Number('abc')的结果为NaN
      console.log(typeof Infinity)
      console.log(typeof Number.MAX_SAFE_INTEGER)

2、typeof返回值为bigint

      console.log(typeof 5n)
      console.log(typeof BigInt(1))

js中的Number类型只能安全地表示-9007199254740991 (-(2^53-1)) 9007199254740991(2^53-1)之间的整数,超出这个范围可能会丢失精度,比如9007199254740992 === 9007199254740993的结果为true,要表示这种超出Number范围的数字就要使用bigint了,在一个数字后面加上n或者使用BigInt()可以将一个数字转为bigint。

3、typeof返回值为string

      console.log(typeof '')
      console.log(typeof ' ')
      console.log(typeof 'template')
      console.log(typeof '1')
      console.log(typeof String())
      console.log(typeof typeof 1) // typeof 总是返回一个字符串

4、typeof返回值为boolean

      console.log(typeof true)
      console.log(typeof false)
      console.log(typeof Boolean())
      console.log(typeof !!0)

5、typeof返回值为undefined

      let username
      console.log(typeof undefined)
      console.log(typeof username)
      console.log(typeof document.all) // document.all == undefined为true

6、typeof返回值为symbol

      console.log(typeof Symbol())
      console.log(typeof Symbol('f'))
      console.log(typeof Symbol.iterator)

7、typeof返回值为function

      console.log(typeof function () {})
      console.log(typeof class C {})
      console.log(typeof Number)
      console.log(typeof String)
      console.log(typeof Math.ceil)
      console.log(typeof new Function()) // 使用new调用构造函数时,除了Function,调用其他的构造函数返回值都是对象

8、typeof返回值为object

      console.log(typeof { name: 'xx' })
      console.log(typeof [])
      console.log(typeof new Date())
      console.log(typeof new Number())
      console.log(typeof /regex/)
      console.log(typeof null)

9、typeof的优先级高于二进制运算符

      console.log(typeof 1 + 1) // number1
      console.log(typeof 1 == '1') // false
      console.log(typeof 1 === 'number') // true
      console.log(typeof 1 + 'a') // numbera
      console.log(typeof (1 + 'a')) // string

二、instanceof

语法:object instanceof constructor

instanceof用来检测constructor.prototype是否在object的原型上

      function A() {}
      function B() {}

      const a = new A()

      console.log(a instanceof A)
      console.log(a instanceof Object)
      console.log(A.prototype instanceof Object)

      B.prototype = new A() // 继承
      const b = new B()
      console.log(b instanceof A) // b的原型上有A

三、Object.prototype.toString.call()

      console.log(Object.prototype.toString.call(1)) // [object Number]
      console.log(Object.prototype.toString.call('1')) // [object String]
      console.log(Object.prototype.toString.call(true)) // [object Boolean]
      console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
      console.log(Object.prototype.toString.call(null)) // [object Null]
      console.log(Object.prototype.toString.call([])) // [object Array]
      console.log(Object.prototype.toString.call({})) // [object Object]
      console.log(Object.prototype.toString.call(/a/)) // [object RegExp]
      console.log(Object.prototype.toString.call(new Date())) // [object Date]
      console.log(Object.prototype.toString.call(new Set())) // [object Set]
      console.log(Object.prototype.toString.call(new Map())) // [object Map]
      console.log(Object.prototype.toString.call(new Function())) // [object Function]
      console.log(Object.prototype.toString.call(function () {})) // [object Function]
      console.log(Object.prototype.toString.call(new Error())) // [object Error]
      console.log(Object.prototype.toString.call(window)) // [object Window]
      ;(function () {
        console.log(Object.prototype.toString.call(arguments)) // [object Arguments]
      })()

四、Array.isArray()

      console.log(Array.isArray([]))
      console.log(Array.isArray(new Array()))
      console.log(Array.isArray(Array.prototype))

当检测 Array 实例时,Array.isArray 优于 instanceof,因为 Array.isArray 能检测 iframes