In es6, you can use Array's isArray() method to determine whether a variable is an array. This method is used to determine whether an object is an array type. The syntax is "Array.isArray(obj)"; if the object is an array, it returns true, otherwise it returns false.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Determining whether a variable is an array in ES5 In ES5
, we have at least the following 5 ways to determine whether a value is an array:
var a = []; // 1. Based on instanceof a instanceof Array; // 2. Based on constructor a.constructor === Array; // 3. Based on Object.prototype.isPrototypeOf Array.prototype.isPrototypeOf(a); // 4. Based on getPrototypeOf Object.getPrototypeOf(a) === Array.prototype; // 5. Based on Object.prototype.toString Object.prototype.toString.apply(a) === '[object Array]';
Above, except for Object.prototype.toString
, other methods cannot correctly determine the type of the variable.
You know, the running environment of the code is very complex, and a variable may use all kinds of tricks to confuse its creator. Let's see
var a = { __proto__: Array.prototype }; //Try running the following codes in the console respectively // 1. Based on instanceof a instanceof Array; // => true // 2. Based on constructor a.constructor === Array; // => true // 3. Based on Object.prototype.isPrototypeOf Array.prototype.isPrototypeOf(a); // => true // 4. Based on getPrototypeOf Object.getPrototypeOf(a) === Array.prototype; // => true
above, all four methods will return true. Why? We just manually specified the __proto__ attribute of an object as Array.prototype, which caused the object to inherit the Array object. This irresponsible inheritance method caused the judgment scheme based on inheritance to instantly collapse.
Not only that, we also know that Array is heap data, and the variable only points to its reference address, so the address referenced by the Array object of each page is different. For an array declared in an iframe, its constructor is the Array object in the iframe. If an array x is declared in the iframe and assigned to the variable y of the parent page, then y instanceof Array is used on the parent page, and the result must be false. The last one returns a string, so there is no reference problem. In fact, interactions between multiple pages or systems are only possible with strings.
Determining whether a variable is an array in ES6
. In view of the common use of arrays, the Array.isArray method is added in ES6. Using this method to determine whether a variable is an array is very simple, as follows:
Array.isArray([]); // => true Array.isArray({0: 'a', length: 1}); // => false
In fact, using Object.prototype.toString
to determine the type of a value is also a standard for major mainstream libraries. Therefore the polyfill for Array.isArray usually looks like this:
if (!Array.isArray){ Array.isArray = function(arg){ return Object.prototype.toString.call(arg) === '[object Array]'; }; }