面试中遇到的判断值类型,要区别typeOf,toString()更准确

  Object.prototype.toString.call({a: 1}) // [object Object]
  Object.prototype.toString.call([1, 2, 3]) // [object Array]
  Object.prototype.toString.call(new Date()) // [object Date]
  Object.prototype.toString.call(new RegExp()) // [object RegExp]
  Object.prototype.toString.call(null) // [object Null]
  Object.prototype.toString.call(undefined) // [object Undefined]
  Object.prototype.toString.call(1) // [object Number]
  Object.prototype.toString.call('1') // [object String]
  Object.prototype.toString.call(true) // [object Boolean]
  Object.prototype.toString.call(Symbol()) // [object Symbol]
  Object.prototype.toString.call(BigInt(1)) // [object BigInt]
  Object.prototype.toString.call(function() {}) // [object Function]
  Object.prototype.toString.call(new Error()) // [object Error]

不倒翁cc
1 声望0 粉丝

努力学些