How does js determine whether an object is an Array?

Source: Internet
Author: User
During development, we often need to determine whether an object is of the array type and common methods for detecting the object type in Js.

During development, we often need to determine whether an object is of the array type. In Js, what common methods are used to detect the object type?

Typeof Operator

For Function, String, Number, Undefined, and other types of objects, it is fully competent, but for Array

var arr=new Array("1","2","3","4","5");alert(typeof(arr));  

You will receive the answer to an object, which is a bit disappointing.

Instanceof Operator

In JavaScript, The instanceof operator returns a Boolean value indicating whether the object is an instance of a specific class. Usage: result = object instanceof class, or the array just now, and then, um, success returns true.

var arrayStr=new Array("1","2","3","4","5");alert(arrayStr instanceof Array);  

Summary: It seems that the questions we have discussed today have been answered, but in fact, moving through multiple frames will cause a big problem.

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 statement is not supported under IE, and alert (arr instanceof Array) is available under FF; // false alert (arr. constructor === Array); // false

The returned result is two False values, which are disappointing.

The ECMA-262 wrote

Object. prototype. toString () When the toString method is called, the following steps are taken:

  1. Get the [[Class] property of this object.
  2. Compute a string value by concatenating the three strings "[object", Result (1), and "]".
  3. Return Result (2)

The above Specification defines 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, combined with call, we can obtain the internal attribute [[Class] of any object, and then convert the type detection to string comparison to achieve our goal. Let's take a look at the description of Array in the ECMA standard.

The ECMA-262 wrote

New Array ([item0 [, item1 [,…])

The [[Class] property of the newly constructed object is set to "Array ".

So using this, the third method was launched.

function isArray(obj) {     return Object.prototype.toString.call(obj) === '[object Array]';    }  

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. Maybe you have to ask, why isn't o. toString () directly ()? 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 ":)

Different from the previous solutions, this method solves the problem of cross-frame object construction. After testing, the compatibility of various browsers is also good and can be safely used. The good news is that many frameworks, such as jQuery and Base2, plan to use this method to implement some special types of objects, such as arrays and regular expressions, you don't have to write it yourself.

In addition, Ext3 has been replaced with this method.

isArray : function(v){            return toString.apply(v) === '[object Array]';        }  

This article is available at http://www.nowamagic.net/librarys/veda/detail/1250.

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.