First: How many data structures are there in ES6? What are the specific ones?
Generally speaking, as of ES6, there are 8 kinds of data decomposition: 7 kinds of original data decomposition + 1 kind of complex data decomposition.
Seven kinds of original data deconstruction:
Boolean Null Undefined Number BigInt String Symbol
One kind of complex data deconstruction:
Object
2. How to judge the type of a data
There are many ways to determine the data type of a variable, but some methods are not applicable to any data type.
1: typeof
typeof can be used to determine all basic data types, returning a string whose value is in lowercase form of the type name.
//Six types of raw data console.log(typeof 'a');//"string" console.log(typeof 1);//"number" console.log(typeof true);//"boolean" console.log(typeof Symbol());//"symbol" console.log(typeof undefined);//"undefined" console.log(typeof null);//"object" //object type console.log(typeof {});//"object" console.log(typeof []);//"object" console.log(typeof new Map());//"object" console.log(typeof new Set());//"object" //function type console.log(typeof function () {});//"function"
From the above code, we can see that there are three types of data returned by typeof, which may deserve our special attention:
1: null is the original data type, but typeof(null) gets'object' 2: Array, Set, Map data, typeof results are'object' 3: function is the core of the whole JavaScript language. Using typeof for a function, we get'function'
2: instanceOf
Grammar: object instance of constructor
InstanceOf is used to determine whether an object is an instance of a constructor, and the return value is true or false. The difference between instanceOf and typeof is that instanceOf is only used for object type data:
let fun = new Function(); function Apple() {} let apple = new Apple(); console.log(fun instanceof Function);//true console.log(apple instanceof Apple);//true console.log(apple instanceof Function);//false console.log([] instanceof Array);//true console.log([] instanceof Object);//true console.log(new Map() instanceof Map);//true console.log(new Map() instanceof Object);//true console.log({} instanceof Object);//true
In particular, Array, Set, Map have their own constructors, but Object is the root constructor, so use instanceOf Object for them, and of course return true.
3: constructor
Grammar: o.constructor
The constructor returns the constructor of an object, not a string. Conceptually, it seems to be similar to instanceOf, but after the actual operation, the results are different. Let's first look at the code:
let fun = new Function(); function Apple() {} let apple = new Apple(); console.log(fun.constructor === Function);//true console.log(apple.constructor === Apple);//true console.log(apple.constructor === Function);//false console.log([].constructor === Array);//true console.log([].constructor === Object);//false console.log(new Map().constructor === Map);//true console.log(new Map().constructor === Object);//false console.log({}.constructor === Object);//true
When we get the constructor for Array and Map, we get the most specific constructor, so their constructor is not equal to Object.
4: prototype
Prototype is a prototype object. All instantiated objects inherit the properties and methods on the prototype object.
All objects, such as {}, inherit attributes and methods on Object.prototype; Array, Set, Map are also inherited from Object, so we can also use prototype to determine the type of object:
let fun = new Function(); function Apple() {} let apple = new Apple(); console.log(Object.prototype.toString.call(fun)); //"[object Function]" console.log(Object.prototype.toString.call(apple)); //"[object Object]" console.log(Object.prototype.toString.call([]));//"[object Array]" console.log(Object.prototype.toString.call(new Map()));//"[object Map]" console.log(Object.prototype.toString.call({}));//"[object Object]"