Whether at work or in a job interview, we’ve all encountered the need to determine whether data is an array or not. Today we’ll summarize how many ways there are to determine an array and see which one is the most useful and reliable.
We judge from three angles: mother, father and ancestor.
Judge (mother) by constructor
instanceof
Determines whether an instance belongs to a constructor
let arr = []
console.log(arr instanceof Array) // true
Copy the code
Disadvantages: The underlying principle of instanceof is to check whether the constructor’s Prototype property is present in an instance’s prototype chain. If the instance’s prototype chain changes, it cannot be correctly determined.
let arr = []
arr.__proto__ = function() {}
console.log(arr instanceof Array) // false
Copy the code
constructor
The constructor property of the instance points to the constructor itself.
let arr = []
console.log(arr.constructor === Array) // true
Copy the code
Disadvantages: If arR’s constructor is modified, the correct judgment cannot be made.
let arr = []
arr.constructor = function() {}
console.log(arr.constructor === Array) // false
Copy the code
Judge by the prototype object (Dad)
__ proto __
The instance’s __proto __ points to the constructor’s prototype object
let arr = []
console.log(arr.__proto__ === Array.prototype) // true
Copy the code
Disadvantages: Cannot make correct judgments if the prototype chain of the instance is modified.
let arr = []
arr.__proto__ = function() {}
console.log(arr.__proto__ === Array.prototype) // false
Copy the code
Object.getPrototypeOf()
Object: a method that gets the prototype Object to which an Object belongs
let arr = []
console.log(Object.getPrototypeOf(arr) === Array.prototype) // true
Copy the code
Cons: ditto
Array.prototype.isPrototypeOf()
Array method to determine whether a prototype object is a prototype object of an object
let arr = []
console.log(Array.prototype.isPrototypeOf(arr)) // true
Copy the code
Cons: ditto
Judge by Object’s prototype Object (ancestor)
Object.prototype.toString.call()
The prototype Object has a toString method, which is inherited by all objects by default and returns “[Object type]”. But this method often covered by prototype chain of the same method, need to pass through the Object. The prototype. ToString. Call () to call.
let arr = []
console.log(Object.prototype.toString.call(arr) === '[object Array]') // true
Copy the code
This type is like a birthmark, engraved on the body from birth, so modifying the archetypal chain will not affect it at all.
let arr = []
arr.__proto__ = function() {}
console.log(Object.prototype.toString.call(arr) === '[object Array]') // true
Copy the code
Array.isArray()
Array.isarray () is a new method in ES6 that is used to check Array types.
let arr = []
console.log(Array.isArray(arr)) // true
Copy the code
Modifying the prototype chain does not affect it at all.
let arr = []
arr.__proto__ = function() {}
console.log(Array.isArray(arr)) // true
Copy the code
conclusion
Array. IsArray is the most useful, most reliable, or ES6 fragrance.
Give it a thumbs up when you see it! Gold three silver four coming, I hope today’s article can help you!