JS Judgment Array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.