1.Constructor
Definition in the definition: constructor property returns a reference to the array function that created this object
Is the constructor that corresponds to the return object. It's not exactly the same as instanceof in terms of definition, but the effect is the same.
such as: (a instanceof Array)//a is an instance of array? True or False
(A.constructor = = array)//A constructor for an instance is an array? True or False
A more rigorous and versatile approach:
function IsArray (object) { return object && typeof object=== ' object ' && Array = = Object.constructor;}
2. characteristic judgment method
The above methods have some defects, but to believe that the wisdom of the masses is omnipotent, we can judge its type according to some characteristics of the array.
function IsArray (object) { return object && typeof object=== ' object ' && typeof object.length=== ' number ' && typeof object.splice=== ' function ' && //Determine if the length property is enumerable for arrays Will get false ! ( Object.propertyisenumerable (' length '));}
Having length and splice is not necessarily an array, because you can add attributes to an object, but you cannot enumerate the length property, which is the most important factor to judge.
PS: Here to popularize the propertyIsEnumerable method:
Object. propertyIsEnumerable (Proname)
Determines whether the specified property can be enumerated
Note: If proname exists in object and you can use a for ... In cyclic poor lift, then the propertyIsEnumerable property returns True. The propertyIsEnumerable property returns False if object does not have the specified property, or if the specified property is not enumerable.
The propertyIsEnumerable property does not take into account objects in the prototype chain.
Example:
var a = new Array ("Apple", "banana", "Cactus");d Ocument.write (a.propertyisenumerable (1));
3. The simplest method
function IsArray (o) { return Object.prototype.toString.call (o) = = = ' [Object Array] ';}
JS Judgment Array