Reproduced source https://www.cnblogs.com/echolun/p/10287616.html
I. Introduction
Second, determine whether the object is an array of several ways
1. Analyzing instanceof
let a = []; a instanceof Array; //true let b = {}; b instanceof Array; //false
In the code above, the instanceof operator detects Array.prototype attribute is present on the prototype chain variable a, a is an array apparently has Array.prototype property, it is true.
Problems:
It should be noted, prototype property can be modified, so it is not initially determined to be true it must always be true.
Secondly, the script when we have more global environment, such as html iframe object owns multiple, instanceof verification results may not be as expected, for example:
// Create and add an iframe object body var iframe = document.createElement('iframe'); document.body.appendChild(iframe); // Get an array constructor method iframe object xArray = window.frames[0].Array; // Get an instance of the constructor was arr = new xArray (1,2,3); arr instanceof Array;//false
Cause this problem because the iframe will produce a new global environment, it also has its own Array.prototype properties, properties under different circumstances so that the same is obviously unsafe practices, so Array.prototype! == window.frames [0] .Array.prototype, want to arr instanceof Array is true, you have to ensure arr is created from the original Array constructor when feasible.
2. Analyzing constructor
let a = [1,3,4]; a.constructor === Array;//true
Again, this determination will be a plurality of global environmental problems, and problems caused instanceof same.
// Create and add an iframe tag for the body var iframe = document.createElement('iframe'); document.body.appendChild(iframe); // Get an array constructor method iframe object xArray = window.frames[window.frames.length-1].Array; // Get an instance of the constructor was arr = new xArray (1,2,3); arr.constructor === Array;//false
3. By Object.prototype.toString.call () Analyzing
Object.prototype.toString (). Call () can obtain different types of objects, e.g.
let a = [1,2,3] Object.prototype.toString.call(a) === '[object Array]';//true
It is a powerful place that not only can test whether an array, such as whether it is a function, whether it is digital, etc.
// function test is whether let a = function () {}; Object.prototype.toString.call(a) === '[object Function]';//true // check whether digital let b = 1; Object.prototype.toString.call(a) === '[object Number]';//true
Even for multi global environment, Object.prototype.toString (). Call () can also be processed in line with the expected judgment.
// Create and add an iframe tag for the body var iframe = document.createElement('iframe'); document.body.appendChild(iframe); // Get an array constructor method iframe object xArray = window.frames[window.frames.length-1].Array; // Get an instance of the constructor was arr = new xArray (1,2,3); console.log(Object.prototype.toString.call(arr) === '[object Array]');//true
4. By Array.isArray () Analyzing
Whether Array.isArray () is used to determine the value of a transmission list, it returns a Boolean value.
let a = [1,2,3] Array.isArray(a);//true
Easy to use, but also for the global environment and more, Array.isArray () can accurately judge the same, but there is a problem, Array.isArray () is presented in ES5, that is likely to exist does not support this method before ES5 Happening. How to solve it?
Third, determine the method of final recommendation array
if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; }
Then for several ways to determine the array is also finished, reasonable recommendation is also given, or have any questions welcome to support the wrong place
References:
Determining with absolute accuracy whether or not a JavaScript object is an array