js data type and data type detection

js data type and js data type detection

1. js data type

js data types are mainly divided into two types: one is the basic data type and the other is the reference data type.

1. Basic data types

The basic data types include: Number, String, Boolean, undefined, null, etc.
The basic data types are stored in the stack.

2. Reference data type

These include Object, Function, Array, etc.
Reference data types are stored in the heap.
Note: Function and Array are also a kind of Object. It is considered a special object.

3.ES6 new data types

  • Symbol and BigInt are new data types in ES6:
  • Symbol represents a unique and immutable data type after creation. It is mainly used to solve the problem of possible global variable conflicts.
  • BigInt is a numeric type of data that can represent integers in any precision format. BigInt can safely store and operate large integers, even if the number exceeds the safe integer range that Number can represent.

2. js data type detection

There are four main methods for js data type detection

First define all data types:

        let a = 1; 
        let b = '1';
        let c = undefined
        let d = true
        let f = null
        let obj = Object.create({
    
    })
        let obj1 = {
    
    };
        let arr = new Array();
        let arr1 = [1];
        let fun = function () {
    
    
            console.log("dd");
        }
        let sym = Symbol('您好');
        let bgin = 100n;

1. typeof

*typeof() ** Implementation principle: Use computer binary values ​​​​to detect, the detection speed is very fast,
null, obj and array types cannot be judged, and object is returned.

      console.log(typeof (a)); //number
      console.log(typeof (b)); //string
      console.log(typeof (c));//undefined
      console.log(typeof (d));//boolean
      console.log(typeof (f));//object
      console.log(typeof (obj));//object
      console.log(typeof (obj1));//object
      console.log(typeof (arr));//object
      console.log(typeof (fun));//function
      console.log(typeof (sym));//symbol
      console.log(typeof (bgin));//bigint
      //判断不出null、obj和数组类型,均返回object

2.instanceof

*instanceof determines the data type** Search and detect according to the prototype chain "If the prototype object of the current class appears on the prototype chain of the detection value, the result is true" (
1). Whether the detection object is an Object instance, the results are all true, so it cannot be verified whether it is a standard ordinary object
(2). instanceof cannot handle the detection of primitive value types, and the detection results are all false.

      console.log(a instanceof Number); //false
      console.log(new Number() instanceof Number); //true
      console.log(b instanceof String);//false
      console.log(f instanceof Object);//false
      console.log(obj instanceof Object);//true
      console.log(arr1 instanceof Object);//true
      console.log(arr instanceof Array);//true
      console.log(fun instanceof Function); //true
      // console.log(sym instanceof Object);//true
      // console.log(bgin instanceof BigInt); //true

3. constructor

The principle of constructor is to find the constructor on the prototype object of the variable to determine the data type. Because the
constructor can be easily modified, the detection results are "for reference only."
Undefined and null values ​​cannot be judged because an error will be reported and there is no constructor attribute.

        console.log(a.__proto__); //Number
        console.log(sym.constructor);//ƒ Symbol() { [native code] }
        console.log(a.constructor == Number); //true
        console.log(b.constructor == String); //true
        console.log(d.constructor == Boolean); //true
        console.log(obj.constructor == Object);//true
        obj.constructor = Function; 
        console.log("修改构造函数");
//注意这里修改了obj的构造函数,指向了function,在次进行obj.constructor == Object  时返回false
        console.log(obj.constructor == Object);//false
        console.log(arr.constructor == Array);//true
        console.log(fun.constructor == Function);//true
        console.log(sym.constructor == Symbol);//true
        console.log(bgin.constructor == BigInt);//true

4.Object.prototype.toString.call()

The principle of object.prototype.toString.call() is to modify the this on the object to point to the variable, and then toString is output.
All data types can be accurately determined.

      const toString = Object.prototype.toString;
      console.log(Object.prototype.toString(d));
       //[object Object] 这里展示了正常object的tostring方法是输出自己的构造函数
      console.log(toString.call(a)); //[object Number]
      console.log(toString.call(b));//[object String]
      console.log(toString.call(c));//[object Undefined]
      console.log(toString.call(d));//[object Boolean]
      console.log(toString.call(f)); //[object Null]
      console.log(toString.call(obj));//[object Object]   
      console.log(toString.call(arr));//[object Array]
      console.log(toString.call(fun));//[object Function]
      console.log(toString.call(sym));//[object Symbol]
      console.log(toString.call(bgin));//[object BigInt]

Guess you like

Origin blog.csdn.net/m0_55315930/article/details/130098104
Recommended