In the project development, we will meet the judgment of type, and add or delete some content according to the type of business needs. For example, the data obtained by the front end through the interface is a number type, and we need to convert some text according to the different data returned, such as: 1-> male,2-> female, similar and so on.

Js has those data types

ECMAScript has six simple data types (also known as primitive types or primitive types) : Number, String, Boolean, Undefined, Null, and Symbol. Symbol is an addition to ES6. There is also a complex data type, Object.

typeof

Typeof, the operator that determines the type, is provided in js. Using the Typeof operator on a value returns one of the following strings:

  • ‘undefined’: indicates that the value is defined
  • ‘Boolean ‘: Indicates that the value is Boolean
  • ‘String ‘: indicates the value is a string
  • ‘number’: indicates that the value is a numerical value
  • ‘object’: indicates that the value is an object (not a function), or null
  • ‘function’: indicates that the value is function
  • ‘symbol’: indicates that the value is a symbol

In the above screenshot, you can see that in simple data types, when typeOF is performed on a value, the corresponding type is returned. Only when typeof is performed on a value, object is returned. This is an issue left over from history.

In the first version of javaScript, different objects in javaScript are represented as binary at the bottom, and javaScript will judge the first three zeros of binary as object, while the binary identifier of null is all zeros, so naturally the first three zeros are all zeros. Therefore, Typeof returns ‘object’ citation javaScript You Didn’t Know (Volume 1)

toString

ToString is a method on an Object prototype that, by default, returns the current Object’s [[Class]]. This is an internal property of the form [object Xxx], where Xxx is the type of the object.

For Object objects, toString() returns [Object Object]. For other objects, use either function.prototype.call () or function.prototype.apply ()

constructor

Constructor is a property of prototype. When a function is defined, the JS engine adds the prototype to the function and the constructor property points to the function reference. Overriding prototype therefore loses the original constructor.

This approach is problematic

  1. Null and undefined do not constructor.

2. In addition, if the developer overwrites prototype, the original constructor will be lost. Therefore, in order to standardize development, it is common to assign a new constructor value when overwriting an object prototype to ensure that the type of the object instance is not tampered with.

instanceof

Instanceof is an instanceof B used to determine whether A is an instanceof b. the expression A instanceof B returns true if A is an instanceof B, false otherwise. It’s important to note here that Instanceof tests prototypes,

Instanceof can only be used to determine whether two objects belong to the instance relationship, but not the specific type of an object instance.

conclusion

Combined with the different operations above, it is generally encapsulated toString(). Determine what type the current value is.

  let toString = Object.prototype.toString
 
  let isType = function(val,type) {
   return function(val) {
     return toString.call(val) === `[object ${type}] `}}Copy the code