JavaScript determines whether an object is an array _javascript tips

Source: Internet
Author: User

This article has shared three kinds of JavaScript to determine whether the object is an array of methods,

1. typeof

The first thing we'll think of is using typeof to detect data types, but for function, String, number, undefined, and so on, you can detect using typeof, such as the following code:

function Test () {}
Console.log (typeof 1);//number
Console.log (typeof test);//function 
Console.log ( typeof "Yunxi"); String
Console.log (typeof undefined);//undefined

But for an array or a regular, it's not enough to detect with typeof, because when we detect an array or a regular, the returned type will be an object, as shown in the following code:

Console.log (typeof []); Object
Console.log (typeof/\d+/g);//Object

2. instanceof

It is easy to think of using instanceof to detect whether an object is an instance of an array, which returns a Boolean (Boolean), returns True if it is an array, or false if it is returned; Let's take a look at the following code for the array:

Console.log ([] instanceof Array); True
console.log (/\d+/g instanceof Array);//False

As can be seen on the use of instanceof can really determine whether an example of the array;
3. Constructor Properties

In JavaScript, each object has a constructor property that references the constructor that initializes the object, such as the type of the unknown object, so we can write a method as follows:

function IsArray (obj) {return
  typeof obj = = ' object ' && obj.constructor = = Array
}
//test demo
con Sole.log (IsArray ([])); True
var a = {"A": 1};
Console.log (IsArray (a)); False

var b = [1,2,3];
Console.log (IsArray (b)); True
Console.log (IsArray (/\d+/g));//False

As you can see, by invoking the IsArray method you can also determine whether an example is an array.
We can now see that the use of the Instanceof method and the constructor property for both 2nd and 3rd may seem to determine whether it is an array, but there are also exceptions, such as when using an array in a page across a frame iframe, Because in the different frame iframe, the created array does not share its prototype property with each other, the following code test can be validated ~

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 is not supported under IE, Standard browser firefox,chrome has

console.log (arr);//Print out ["1", "2", "3", "4", "5"]
Console.log (arr Instanceof Array); False 
console.log (arr.constructor = = Array);//False

None of the above methods can be used to determine whether an object is an array; But as we can see in ECMA262, we can use the Object.prototype.toString.call () method to determine whether an object is an array, and the following code:

function IsArray (obj) {return
  Object.prototype.toString.call (obj) = ' [Object Array] ';
}
Code calls
Console.log (IsArray ([]));//True
Console.log (IsArray ([1,2,3]));//True

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");

Console.log (arr); ["1", "2", "3", "4", "5"]
Console.log (IsArray (arr));

The above is the entire content of this article, to help you learn JavaScript to determine whether the object is an array of methods, I hope to help you learn.

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.