Javascript method for judging an object as an array, javascript Array

Source: Internet
Author: User

Javascript method for judging an object as an array, javascript Array

How javascript judges an object as an array

Array object

Js arrays are non-typed: array elements can be of any type, and different elements in the same Array may have different types. The elements of an array can be objects or other arrays, so that you can create a complex data structure.

We can usually use the unary operator typeof to determine the js data type, but for a special object such as an array, only "object" can be returned"

typeof [1,2,3]"object"typeof 100"number"typeof false"boolean"typeof undefined"undefined"typeof NaN"number"typeof function(){}"function"typeof null"object"

How to judge an array

Instanceof

Instanceof is a binary operator. The left operand is an object. If not, false is returned. The right operand is a function object or function constructor. If not, false is returned. The principle is to determine whether the prototype attribute of the constructor of the right operand exists on the prototype of the object with the left operand.

[1,2] instanceof Array true

Array. isArray (arr)

An Array method added in ES5, which is a static function of the Array object to determine whether an object is an Array.

Array.isArray([1,2])true

If there are n frames in the page, multiple Windows exist, and each window has its own Array object. For example, when determining whether an Array in a subwindow is an Array, this method cannot be used with instanceof.

Var fr = window. frames [0]; fr. onload = function () {console. log (fr. arr instanceof Array); // false console. log (Array. isArray (fr. arr); // true // arr is an array of another page}

Object. prototype. toString. call (arr) === "[object Array]"

Object.prototype.toString.call([1,2])"[object Array]"

Arr. constructor. name = 'array'

[1,2].constructor.name==='Array';true

However, the constructor attribute of the object can be rewritten. After rewriting, the constructor attribute cannot be determined by the modification method.

var arr=[1,2];arr.constructor={};arr.constructor.name === "Array" //undefinedfalse

Other methods can use some unique methods of the array to determine whether the object is an array, such as join and push.

var c=[1,2];c.push('3');//3console.log(c)[1, 2, "3"]var c="12";c.push('3');//Uncaught TypeError: c.push is not a function(…)var c=[1,2];c.join('');"12"var c='12';c.join('');//Uncaught TypeError: c.join is not a function(…)

Summary

Through the above several methods to determine the object as an Array object, it is better to use Array. isArray (arr) and Oblect. prototype. toString. call (arr.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.