Method for judging whether a variable is of the Array type

Source: Internet
Author: User

In many cases, we need to judge the array type of a variable. In JavaScript, how does one determine whether a variable is of the Array type? I have recently studied and shared with you, hoping to help you.

Methods for detecting objects in JavaScript
1. typeof Operator
This method is not stressful for some common types, such as Function, String, Number, and Undefined, but does not work if the Array object is detected.
Copy codeThe Code is as follows:
Alert (typeof null); // "object"
Alert (typeof function (){
Return 1;
}); // "Function"
Alert (typeof 'menglong xiaozhan '); // "string"
Alert (typeof 1); // "number"
Alert (typeof a); // "undefined"
Alert (typeof undefined); // "undefined"
Alert (typeof []); // "object"

2. instanceof Operator
This operator has a relationship with object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. This operator is used to check whether the prototype object of an object is directed to the prototype object of the constructor.
Var arr = [1, 2, 3, 1];
Alert (arr instanceof Array); // true
3. constructor attribute of the object
In addition to instanceof, each object also has the constructor attribute, which can also be used to determine the Array.
Copy codeThe Code is as follows:
Var arr = [1, 2, 3, 1];
Alert (arr. constructor === Array); // true

2nd and 3rd methods seem impeccable, but in fact there are still some loopholes. When you move back and forth between multiple frames, the two methods will be Alexander. Since each iframe has its own execution environment, objects instantiated across frames do not share the prototype chain with each other, so the above detection code becomes invalid!
Copy codeThe Code is as follows:
Var iframe = document. createElement ('iframe'); // create an iframe
Document. body. appendChild (iframe); // Add it to the body
XArray = window. frames [window. frames. length-1]. Array;
Var arr = new xArray (, 3); // declare an array [, 3]
Alert (arr instanceof Array); // false
Alert (arr. constructor === Array); // false

Method for Detecting array types
The above methods seem impeccable, but there will be some problems in the end. Next we will provide you with some good methods, which can be said to be impeccable.
1. Object. prototype. toString
Object. prototype. toString behavior: first, obtain an internal attribute of the object [[Class], and then, based on this attribute, returns a string similar to "[object Array]" as the result (we should all know after reading the ECMA standard, [[] is used to indicate the attributes used inside the language and which cannot be accessed externally, is called "Internal attribute "). With this method and call, we can obtain the internal attribute [[Class] of any object and convert the type detection to string comparison to achieve our goal.
Copy codeThe Code is as follows:
Function isArrayFn (o ){
Return Object. prototype. toString. call (o) = '[object Array]';
}
Var arr = [1, 2, 3, 1];
Alert (isArrayFn (arr); // true

Call changes the this reference of toString to the object to be checked, returns the string representation of the object, and then compares whether the string is '[ object Array] 'to determine whether it is an Array instance. Why not o. toString ()? Well, although Array inherits from Object, there will also be the toString method, but this method may be rewritten and cannot meet our requirements, and Object. prototype is the tiger's ass, and few people dare to touch it, so it can be "pure" to a certain extent ":)

The value of [[Class] can only be one of the following strings: Arguments, Array, Boolean, Date, Error, Function, JSON, Math, Number, object, RegExp, String.
This method is often useful when identifying built-in objects, but do not use this method for custom objects.
2. Array. isArray ()
ECMAScript5 officially introduces Array. isArray () to JavaScript to accurately detect whether a value is an Array. IE9 +, Firefox 4 +, Safari 5 +, Opera 10.5 +, and Chrome all implement this method. However, versions earlier than IE8 are not supported.
3. Good reference
Based on the above several methods, there is an optimal way to judge the current array:
Copy codeThe Code is as follows:
Var arr = [1, 2, 3, 1];
Var arr2 = [{abac: 1, abc: 2}];
Function isArrayFn (value ){
If (typeof Array. isArray = "function "){
Return Array. isArray (value );
} Else {
Return Object. prototype. toString. call (value) = "[object Array]";
}
}
Alert (isArrayFn (arr); // true
Alert (isArrayFn (arr2); // true

In JavaScript, how does one determine whether a variable is of the Array type? The above is a JavaScript method that I will share with you to judge whether a variable is of the Array type. I hope it will be helpful to you.

Related Article

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.