Type judgment

Time:2021-1-16
Type of type judgment

Typeof can only judge the basic type and object reference type in JavaScript. For all kinds of objects, it can only roughly judge the object type, but not the specific type of the object. So the types that typeof can judge are number, string, Boolean, symbol, undefined, bigint, object

Note: the result of typefo null is of object type, but in fact null is not of object type, so it is null type. The reason why it can get such a result is due to the error in JavaScript typeof behavior.

// 1. typeof

let symbol = Symbol()

typeof 23                       // number
typeof "Jason"                  // string
typeof true                     // boolean
typeof undefined                // undefined 
typeof symbol                   // symbol
typeof 9007199254740991n        // bigint


//Do not judge the specific type of the object
typeof {}               // object
typeof []               // object
typeof new Error()      // object
typeof null             // object

typeof function(){}     // function

Note: function functions belong to the object type, but when typeof function, it returns a function instead of an object. This is because typefo treats function functions differently. In fact, there is no function type. This is an early error of JavaScript, but it is very convenient for programming.

Instanceof type judgment

Instanceof determines the specific type of an object. Instanceof checks the prototypes of the source chain of the target object and the prototypes of the specified type. As long as there are prototypes of the specified type in the prototype chain, it returns true. Otherwise, it returns false. Instanceof is not so limited as typeof. It can check and judge all kinds of objects of the object.

new Date() instanceof Date                              // true
new String() instanceof String                          // true
new Number() instanceof Number                          // true
new Error() instanceof Error                            // true
function(){} instanceof Function                        // true
new Map() instanceof Map                                // true
new Set() instanceof Set                                // true
new RegExp('ab+c', 'i') instanceof RegExps              // true

....

Note: null has no prototype, so it can’t be judged by this method

Object.prototype.toString () type judgment

Various types or object calls Object.prototype.toString () method will return the string of [object type], which can be used to determine various data types. Only the returned string may be a little difficult to operate in the actual programming application, so we need a function to process the returned result after judgment.

let res= {}

  "Null Undefined String Number Boolean Function Date RegExp Error Array Object Math Class".split(" ").map(item =>{
    res["[object "+ item + "]"] = item.toLowerCase();
  })
  
    function  type(obj){
    return typeof obj === "object" || typeof obj === "function" ? res[Object.prototype.toString.call(obj)] || "object" : typeof obj;
  }
  
  //Testing
  let date = new Date();
  function f(){}
  
  
  type(date);       // date
  type(f);          // function
  type([])          // array