JavaScript tips for determining whether an object is an array _ javascript

Source: Internet
Author: User
This article mainly introduces three methods for JavaScript to determine whether an object is an array. Interested friends can refer to the article to share with you three methods for JavaScript to determine whether an object is an array,

1. typeof

The first thing we will think of is to use typeof to detect data types. However, for basic types such as Function, String, Number, and Undefined, typeof can be used to detect data types, for example, the Code is as follows:

function test(){}console.log(typeof 1); // numberconsole.log(typeof test); // function console.log(typeof "yunxi"); // stringconsole.log(typeof undefined); // undefined

But for arrays or regular expressions, if typeof is used for detection, it cannot be satisfied, because when we detect arrays or regular expressions, the returned type will be an object, the following code is used:

console.log(typeof []); // objectconsole.log(typeof /\d+/g); // object

2. Instanceof

Therefore, we can easily think of using instanceof to check whether an object is an array instance. This check returns a boolean value. If it is an array, true is returned, otherwise, false is returned. Let's take a look at the code for checking whether the array is as follows:

console.log([] instanceof Array); // trueconsole.log(/\d+/g instanceof Array); // false

As shown above, instanceof can be used to determine whether it is an array column;
3. constructor attributes

In javascript, each object has a constructor attribute, which references the constructor that initializes the object. For example, to determine the type of an unknown object, we can write a method as follows:

Function isArray (obj) {return typeof obj = 'object' & obj. constructor = Array} // test democonsole. log (isArray ([]); // truevar a = {"a": 1}; console. log (isArray (a); // falsevar B = [1, 2, 3]; console. log (isArray (B); // trueconsole. log (isArray (/\ d +/g); // false

As shown above, you can call the isArray method to determine whether it is an array column.
We can now see that the instanceof method and the constructor attribute can both be used to determine whether it is an array, but there are also external columns, for example, when using an array on the page during cross-frame iframe, it will fail, because in Different frames iframe, the created array will not share its prototype attribute with each other; the following code can be verified after testing ~

Var iframe = document. createElement ('iframe'); document. body. appendChild (iframe); xArray = window. frames [window. frames. length-1]. array; var arr = new xArray ("1", "2", "3", "4", "5"); // This method is not supported in IE, the standard browser firefox and chrome has a console. log (arr); // print the ["1", "2", "3", "4", "5"] console. log (arr instanceof Array); // false console. log (arr. constructor === Array); // false

None of the above methods can determine whether an object is an array. However, we can see in ECMA262 that we can useObject. prototype. toString. call () methodTo determine whether an object is an array. The following code:

Function isArray (obj) {return Object. prototype. toString. call (obj) = '[object Array]';} // The Code calls the console. log (isArray ([]); // trueconsole. log (isArray ([1, 2, 3]); // truevar iframe = document. createElement ('iframe'); document. body. appendChild (iframe); xArray = window. frames [window. frames. length-1]. array; var arr = new xArray ("1", "2", "3", "4", "5"); console. log (arr); // ["1", "2", "3", "4", "5"] console. log (isArray (arr); // true

The above is all the content in this article. It helps you to learn how to determine whether an object is an array in JavaScript, and I hope it will be helpful for your learning.

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.