🎯 summary

  • typeofThe operator
  • instanceofThe operator
  • Object.prototype.toString.call()methods

The typeof operator

  1. Returns one for type determinationstring.
    • String ➡ ️'string'
    • Numerical ➡ ️'number'
    • Boolean value ➡ ️'boolean'
    • Undefined ➡ ️'undefined'
    • Null ➡ ️'object'
    • Symbol value ➡ ️'symbol'
    • General function, arrow function, Generator function, async function, class ➡️'function'
    • Other objects ➡️'object'
  2. conclusion
    • Accurately identify basic data type values (other than NULL)
    • Functions can be identified from reference data type values
    • A reference datatype value other than a function cannot be accurately identified, but instead is identified as'object'.

The instanceof operator

  1. Used to check whether an object is an instance of the given constructor, returning true if, false otherwise.

    • Basic data type value instanceof Arbitrary constructor ➡️false
    • Instanceof Original reference datatype /Object ➡️true
    • Instances of user-defined reference data types instanceof user-defined reference types/ancestor classes of user-defined reference types /Object ➡️true
  2. For the last point above, always return true as long as the constructor/class is defined and the entire prototype of the constructor/class is not modified. Here’s a counter example:

    function Animal() {}
    function Cat() {}
    Cat.prototype = new Animal()
    
    let cat1 = new Cat()
    console.log(cat1 instanceof Cat)  // true
    console.log(cat1 instanceof Animal)  // true
    console.log(cat1 instanceof Object)  // true
    
    // Changed the Cat constructor's entire prototype
    Cat.prototype = {}
    console.log(cat1 instanceof Cat)  // false
    console.log(cat1 instanceof Animal)  // true
    console.log(cat1 instanceof Object)  // true
    
    let cat2 = new Cat()
    console.log(cat2 instanceof Cat)  // true
    console.log(cat2 instanceof Animal)  // false
    console.log(cat2 instanceof Object)  // true
    Copy the code
  3. conclusion

    • Basic data type values cannot be recognized
    • Raw reference data type values can be accurately identified
    • Custom reference data type values can be recognized, but the most precise constructor, class, cannot be located.
  4. Check whether there is a prototype object of the given constructor on the prototype chain of an object, if so, return true.

  5. The following simulation implements the instanceof operator

    function isInstanceOf(instance, constructor) {
      const list = ['string'.'number'.'boolean'.'undefined'.'symbol']
      const type = typeof instance
      const isBasicVal = instance === null || list.indexOf(type) > -1
    
      const prototype = constructor.prototype
      let proto = isBasicVal ? null : instance.__proto__
    
      while (proto) {
        if (proto === prototype) {
          return true
        }
        proto = proto.__proto__
      }
      return false
    }
    Copy the code

Object. The prototype. ToString. Call () method

  1. For type determination, returns a string representing the data type.

    • String, String instance ➡️'[object String]'
    • Value and Number Example ➡️'[object Number]'
    • Boolean, Boolean instance ➡️'[object Boolean]'
    • Undefined ➡ ️'[object Undefined]'
    • Null ➡ ️'[object Null]'
    • Symbol value ➡ ️'[object Symbol]'
    • General functions, arrow functions, class ➡️'[object Function]'
    • The Generator function ➡ ️'[object GeneratorFunction]'
    • Async function ➡ ️'[object AsyncFunction]'
    • The Date instance ➡ ️'[object Date]'
    • Regular expression ➡️'[object RegExp]'
    • Array ➡ ️'[object Array]'
    • Object instance, instance of custom reference data type ➡️'[object Object]'
  2. Take the last point as an example:

    / / Object instance
    console.log(Object.prototype.toString.call({}) === '[object Object]')  // true
    // A custom instance of a reference data type
    function Animal() {}
    console.log(Object.prototype.toString.call(new Animal()) === '[object Object]')  // true
    class Cat(a){}
    console.log(Object.prototype.toString.call(new Cat()) === '[object Object]')  // true
    Copy the code
  3. conclusion

    • Basic data type values are recognized, but string, numeric, and Boolean values are indistinguishable from their respective wrapper types.
    • You can accurately identify the original reference data type values, noting the type distinction between Generator and async functions.
    • A custom reference data type value is not recognized, but instead is recognized as'[object Object]'.