1.列举 JavaScript 中的类型,有什么类型判断的方法?

  1. "原生类型"
  2. Boolean
  3. Number
  4. Null
  5. Undefined
  6. Symbol
  7. String
  8. Bigint
  9. "引用类型"
  10. Object
  • 最常见的判断方法:typeof

    1. console.log(typeof 1) // number
    2. console.log(typeof 'test') // string
    3. console.log(typeof true) // boolean
    4. console.log(typeof undefined) // undefined
    5. console.log(typeof null) // object
    6. console.log(typeof {}) // object
    7. console.log(typeof []) // object
    8. console.log(typeof Symbol()) // symbol
  • 判断已知对象类型的方法:instanceof

instanceof操作符用于检查一个对象是否书友某个特定的class,同时它还考虑了继承.
A instanceof B表示判断A是否位B的实例,如果是返回true

instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型。

  • 根据对象的constructor判断

A.constructor === B
在实际情况下,constructor属性可以被随意修改,而且你在原型继承中,很容易忽略调constructor的指向

  1. function Rabbit (name) {
  2. Animal.call(this,name);
  3. }
  4. Rabbit.prototype = Object.create(Animal.prototype);
  5. //需要说动设置construct的正确指向
  6. Rabbit.prototype = constructor = Rabbit;
  7. const rabbit = new Rabbit('小兔');
  8. rabbit.contructor === Rabbit //true
  • 通用方法: prototype

toString()方法能识别是因为引擎为它们设置好了toStringTag标签

  1. console.log(Object.prototype.toString.call('')) // [object String]
  2. console.log(Object.prototype.toString.call(1)) // [object Number]
  3. console.log(Object.prototype.toString.call(true)) // [object Boolean]
  4. console.log(Object.prototype.toString.call(Symbol())) //[object Symbol]
  5. console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
  6. console.log(Object.prototype.toString.call(null)) // [object Null]
  7. console.log(Object.prototype.toString.call(new Function())) // [object Function]
  8. console.log(Object.prototype.toString.call(new Date())) // [object Date]
  9. console.log(Object.prototype.toString.call([])) // [object Array]
  10. console.log(Object.prototype.toString.call(new RegExp())) // [object RegExp]
  11. console.log(Object.prototype.toString.call(new Error())) // [object Error]
  12. console.log(Object.prototype.toString.call({})) // [object Object]
  13. console.log(Object.prototype.toString.call(Math)) // [object Math]
  14. console.log(Object.prototype.toString.call(JSON)) // [object JSON]
  15. function a () {
  16. console.log(Object.prototype.toString.call(arguments)) // [object Arguments]
  17. }
  18. a()
  19. class ValidatorClass {}
  20. let newClass = new ValidatorClass();
  21. console.log(Object.prototype.toString.call(newClass));[object Object]

而自己创建的类却不会,toString()找不到toStringTag属性只好返回Object标签

  • 万能方法:jquery.type()

    1. //如果对象是undefined或null,则返回相应的“undefined”或“null”。
    2. jQuery.type( true ) === "boolean"
    3. jQuery.type( 3 ) === "number"
    4. jQuery.type( "test" ) === "string"
    5. jQuery.type( function(){} ) === "function"
    6. jQuery.type( [] ) === "array"
    7. jQuery.type( new Date() ) === "date"
    8. jQuery.type( new Error() ) === "error" // as of jQuery 1.9
    9. jQuery.type( /test/ ) === "regexp"

    2.结合类型判断的各种方法,手写一个 getType 函数

  • 参数:value,任何类型,一个值

  • 返回值:String 类型,value 的类型
  • 要求:

null => ‘null’
Boolean 原始值 => ‘boolean’
Boolean 对象(new Boolean(true))=> ‘Boolean’
特殊对象(Math)=> ‘Math’

  1. function getType(value) {
  2. let get = Object.prototype.toString.call(value).replace('[object ','').replace(']','')
  3. //如果是引用类型的直接返回,是原始类型的返回小写
  4. if(typeof value === 'object') return get;
  5. else return get.toLowerCase()
  6. }
  7. console.log(getType(true));
  1. 模拟 instanceof
  1. function instanceOf(object, constructor) {
  2. if(typeof object !== 'object' || constructor === 'null') return false;
  3. //拿到原型
  4. let proto = Object.getPrototypeOf(object)
  5. //顺着原型链去找,直到找到相同的原型对象,返回true,否则为false
  6. while(1) {
  7. if(proto === null) return false;
  8. if(proto === constructor.prototype) return true;
  9. proto = Object.getPrototypeOf(proto)
  10. }
  11. }
  1. 阐述 Object.prototype.toString.call().slice(8,-1) 原理
    1. toString()方法,返回对象的字符串表现
    2. 数组,方法等Object的实例,都各自重写了toString方法
    3. 要得到对象的具体类型,需要调用Object的原型的未被重写的toString方法
    4. call的原因是调转指针,将被call的指向Object.prototype指向的(子项通过call调用了原父项的toString方法)