Judge data in JavaScript is what kind of method is: typeof, instanceof, constructor, the Object, prototyee. ToString method and Array. IsArray (), etc.

1. Typeof operator – this returns a string representing the parameter data type

Typeof [1,2] --> "object" typeof {a:1} --> "object" typeof null --> "object" typeof {a:1} --> "object" typeof null --> "object"Copy the code

So typeof can’t be used as a way to judge arrays

2. Instanceof operator – Used to check if the constructor’s prototype property is present in the prototype chain of an instance object

// instanceof constructor [1,2] instanceof Array --> true [1,2] Instanceof Object --> true // The Object constructor {} instanceof Array --> false can also be found on the Array prototype chainCopy the code

3. Constructor property – This property points to the method that generated the value

ƒ --> ƒ Array() {[native code]} {}. Constructor --> ƒ Object() {[native code]} nullCopy the code

Constructor is not a particularly reliable method because the constructor property can be overridden

4. Object. The prototype. ToString – returns “[Object type]”, type represents the type of Object

Object.prototype.toString.call([]) --> "[object Array]" Object.prototype.toString.call({}) --> "[object Object]" Object. The prototype. ToString. Call (' a ') - > "[Object String]" / / can also apply methodCopy the code

5. Use the isArray method – return true if the argument isArray, false otherwise