JS determine whether the four approaches array (reproduced)

Reproduced source https://www.cnblogs.com/echolun/p/10287616.html

I. Introduction

How to determine whether an object or a value is an array, in an interview or work, we often encounter this problem, since the emergence of high-frequency, or be thinking about finishing, then this article is based on several main judge and the manner in judgment principle, if there are problems to discuss.

Second, determine whether the object is an array of several ways

1. Analyzing instanceof

instanceof operator to test prototype property anywhere constructor chain is present in the prototype object, and returns a Boolean value.
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:

Copy the code
// 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
Copy the code

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

We know that the constructor property constructor instance constructors points, then by the constructor property can also determine whether an array.
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.

Copy the code
// 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
Copy the code

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.

Copy the code
// 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
Copy the code

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

 Of course, with Array.isArray (), new from ES5 isArray () method to determine precisely in order to arrays provide a stable method available, good things can not be raised without specifically for this purpose, and for ES5 before the problem with this approach is not supported we can actually do a good job compatible conduct its own package, like this:
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

Guess you like

Origin www.cnblogs.com/hao-1234-1234/p/11851471.html
Recommended