粗粒度

===

缺点
只能判断出undefined和null 类型

typeof

返回字符串类型的结果值
缺点
无法具体区分出object类型

  1. typeof 1 // 'number'
  2. typeof '1' // 'string'
  3. typeof undefined // 'undefined'
  4. typeof true // 'boolean'
  5. typeof Symbol() // 'symbol'
  6. typeof null // 'object'
  7. typeof [] // 'object'
  8. typeof {} // 'object'
  9. typeof console // 'object'
  10. typeof console.log // 'function'

instanceof

作用
判断前值是否是后值的构造函数生成的实例

  1. let Car = function() {}
  2. let benz = new Car()
  3. benz instanceof Car // true
  4. let car = new String('Mercedes Benz')
  5. car instanceof String // true
  6. let str = 'Covid-19'
  7. str instanceof String // false
  8. "abc" instanceof String // false
  9. "Abc" instanceof Object // false
  10. null instanceof Object // false
  11. {} instanceof Object // Uncaught SyntaxError: Unexpected token 'instanceof'
  12. ({}) instanceof Object // true
  13. [] instanceof Array // true
  14. [] instanceof Objcet // true
  15. var b1 = {
  16. b2:[1,'abc',console.log],
  17. b3:function(){
  18. console.log(123)
  19. }
  20. }
  21. b1 instanceof Object // true
  22. b1.b2 instanceof Object // true
  23. b1.b2 instanceof Array // true
  24. b1.b3 instanceof Function // true
  25. b1.b3 instanceof Object // true

instanceof原理

https://www.yuque.com/docs/share/b45c44ca-626e-40dc-8be0-07abcb706ce9?#

手写instanceof

  1. function myInstanceof(left, right) {
  2. // 这里先用typeof来判断基础数据类型,如果是,直接返回false
  3. if(typeof left !== 'object' || left === null) return false;
  4. // getProtypeOf是Object对象自带的API,能够拿到参数的原型对象
  5. let proto = Object.getPrototypeOf(left);
  6. while(true) { //循环往下寻找,直到找到相同的原型对象
  7. if(proto === null) return false;
  8. if(proto === right.prototype) return true;//找到相同原型对象,返回true
  9. proto = Object.getPrototypeof(proto);
  10. }
  11. }
  12. // 验证一下自己实现的myInstanceof是否OK
  13. console.log(myInstanceof(new Number(123), Number)); // true
  14. console.log(myInstanceof(123, Number)); // false

细粒度

Array.isArray(value)

Object.prototype.toString

  1. Object.prototype.toString({}) // "[object Object]"
  2. Object.prototype.toString.call({}) // 同上结果,加上call也ok
  3. Object.prototype.toString.call(1) // "[object Number]"
  4. Object.prototype.toString.call('1') // "[object String]"
  5. Object.prototype.toString.call(true) // "[object Boolean]"
  6. Object.prototype.toString.call(function(){}) // "[object Function]"
  7. Object.prototype.toString.call(null) //"[object Null]"
  8. Object.prototype.toString.call(undefined) //"[object Undefined]"
  9. Object.prototype.toString.call(/123/g) //"[object RegExp]"
  10. Object.prototype.toString.call(new Date()) //"[object Date]"
  11. Object.prototype.toString.call([]) //"[object Array]"
  12. Object.prototype.toString.call(document) //"[object HTMLDocument]"
  13. Object.prototype.toString.call(window) //"[object Window]"

封装全类型检测函数

  1. function getType(obj){
  2. let type = typeof obj;
  3. if (type !== "object") { // 先进行typeof判断,如果是基础数据类型,直接返回
  4. return type;
  5. }
  6. // 对于typeof返回结果是object的,再进行如下的判断,正则返回结果
  7. return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1'); // 注意正则中间有个空格
  8. }
  9. /* 代码验证,需要注意大小写,哪些是typeof判断,哪些是toString判断?思考下 */
  10. getType([]) // "Array" typeof []是object,因此toString返回
  11. getType('123') // "string" typeof 直接返回
  12. getType(window) // "Window" toString返回
  13. getType(null) // "Null"首字母大写,typeof null是object,需toString来判断
  14. getType(undefined) // "undefined" typeof 直接返回
  15. getType() // "undefined" typeof 直接返回
  16. getType(function(){}) // "function" typeof能判断,因此首字母小写
  17. getType(/123/g) //"RegExp" toString返回