JavaScript data types and type detection

1. There are seven data types, six simple data types and one complex data type.
  1. Simple data types: String, Number, Boolean, Null, Undefined, Symbol.
  2. Complex data types: Object is the only complex data type. Array Function these reference type values ultimately boil down to the Object complex data type.
2. Special basic type: basic packaging type

To facilitate manipulation of primitive type values, ECMAscript provides three special reference types: Boolean, Number, and String. Every time a primitive type value is read, an object of the corresponding primitive wrapper type is created behind the scenes, allowing us to call methods to manipulate the data.

var s1 = "some text";
// here we create a string stored in s1, which of course is a primitive value.
// But now we are calling s1's method. Primitive values are not objects, so there should be no methods.
// In order for us to achieve this intuitive operation, the background has already helped us complete a series of operations. When we access the S1 variable in the second line of code, the access is in read mode, and when we access the string in read mode, the following is automatically done in the background.
var s2 = s1.substring(2);
/* 1. Create an instance of String; 2. Invoke the specified method on the instance. 3. Destroy the instance. var s1 = new String("some text"); var s2 = s1.substring(2); s1 = null; * /
Copy the code
3. Detection methods of data types
  1. Typeof: Returns correctly for all types except null (returns OBEJCT) for base types. Return object for complex types except function (return function).

  2. Instanceof: The instanceof operator tests whether an object has a constructor’s Prototype property in its prototype chain. (That is, it can only be used to determine whether two objects belong to the instance relationship, but not the specific type of an object instance).

     var str = 'text';
     str instanceof String;  // false (wrapper type)
     var arr = [1.2.3];
     arr instanceof Array;   // true
     arr instanceof Object;  // true
     // Returns true because the Object constructor's prototype property is linked to the array instance arr.
    Copy the code
  3. Use the object’s constructor to determine the object’s type. (Error: js detects null or undefined’s constructor property! Null and undefined have no constructor attribute at all.)

    function cstor(variable) {
     if (variable === null || variable === undefined) {
         return 'Null or Undefined';
     }
     
     var cst = variable.constructor;
     
     switch (cst) {
         case Number:
             return 'Number'
         case String:
             return 'String'
         case Boolean:
             return 'Boolean'
         case Array:
             return 'Array'
         case Object:
             return 'Object'}}Copy the code
  4. Object. The prototype. ToString. Call () : access to all objects with the [[Class]] this internal attributes, return a string “[Object Class]”.

    /** * @param type string, the string of the type to be tested * @return type detection function * based on the data type passed in, returns the type detection function for that type * type detection using toString function */
    function isType(type) {
      return function (val) {
        if (Object.prototype.toString.call(val) === `[object ${type}] `) {
          return true;
        }
        return false;
      };
    }
    
    export let isString = isType('String');
    export let isArray = isType('Array');
    export let isFunction = isType('Function');
    export let isObject = isType('Object');
    export let isNumber = isType('Number');
    Copy the code

    Front-end basic Skills series: Welcome to learn together:Github.com/ruralist-si…