Various methods for judging object types using JavaScript isArray () Functions

Source: Internet
Author: User

1) typeof Operator
Typeof is a unary operator. The returned result is a string indicating the number of operations. For example, "number", "string", "boolean", "object", "function", and "undefined" (used to determine whether a variable exists ).
However, typeof has limited capabilities. For Date and RegExp types, "object" is returned ". For example:
Copy codeThe Code is as follows:
Typeof {}; // "object"
Typeof []; // "object"
Typeof new Date (); // "object"

So it is only useful when distinguishing objects from the original type. To partition one object type and another object type, you must use other methods. For example, instanceof operator or constructor of an object.

2) instanceof operator.
The instanceof operator requires that the Operation number on the left is an object, and the operation number on the right is the name or constructor of the object class. If the object is a class or constructor instance, The instanceof operator returns true. If the object is not an instance of the specified class or function, or the object is null, false is returned. For example:

[] Instanceof Array; // true
[] Instanceof Object; // true
[] Instanceof RegExp; // false
New Date instanceof Date; // true

Therefore, you can use the instanceof operator to determine whether the object is of the array type:

Function isArray (arr)
{
Return arr instanceof Array;
}

3) constructor attribute.
In JavaScript, each object has a constructor attribute, which references the constructor that initializes the object and is often used to determine the type of an unknown object. If a knowledge value is given, the typeof operator is used to determine whether it is the original value or an object. If it is an object, you can use the constructor attribute to determine its type. Therefore, the function used to judge the array can also be written as follows:

Function isArray (arr)
{
Return typeof arr = "object" & arr. constructor = Array;
}

In many cases, we can use the instanceof operator or the constructor attribute of an object to check whether the object is an array. For example, many JavaScript frameworks use these two methods to determine whether an object is of the array type.
However, it fails to detect arrays on the cross-frame page. The reason is that the arrays created in different frameworks (iframe) do not share their prototype attributes with each other. For example:
Copy codeThe Code is as follows:
<Script>
Window. onload = function (){
Var iframe_arr = new window. frames [0]. Array;
Alert (iframe_arr instanceof Array); // false
Alert (iframe_arr.constructor = Array); // false
}
</Script>
<Body>
<Iframe> </iframe>
</Body>

On Ajaxian, we can see a precise detection method. The toString () method is called across the prototype chain: Object. prototype. toString (). The preceding cross-framework issues can be solved.

After Object. prototype. toString (o) is executed, perform the following steps:
1) obtain the class attribute of object o.
2) connection string: "[object" + Result (1) + "]"
3) returned results (2)

For example:

Object. prototype. toString. call ([]); // return "[object Array]"
Object. prototype. toString. call (/reg/ig); // return "[object RegExp]"

In this way, we can write a robust function to determine whether an object is an array:
Copy codeThe Code is as follows:
Function isArray (arr)
{
Return Object. prototype. toString. call (arr) = "[object Array]";
}

This method has been recognized by many foreign javaScript masters. This method will be used to detect arrays in the upcoming release of jQuery 1.3.

A Maintainer of prototype. js writes the following function to obtain the object type name.
Copy codeThe Code is as follows:
/**
* Returns internal [[Class] property of an object
*
* Ecma-262, 15.2.4.2
* 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 ).
*
* _ GetClass (5); // => "Number"
* _ GetClass ({}); // => "Object"
* _ GetClass (/foo/); // => "RegExp"
* _ GetClass (''); // =>" String"
* _ GetClass (true); // => "Boolean"
* _ GetClass ([]); // => "Array"
* _ GetClass (undefined); // => "Window"
* _ GetClass (Element); // => "Constructor"
*
*/
Function _ getClass (object)
{
Return Object. prototype. toString. call (object). match (/^ \ [object \ s (. *) \] $/) [1];
};

Extended to detect various object types:
Copy codeThe Code is as follows:
Var is =
{
Types: ["Array", "Boolean", "Date", "Number", "Object", "RegExp", "String", "Window", "HTMLDocument"]
}

For (var I = 0, c; c = is. types [I ++];)
{
Is [c] = (function (type)
{
Return function (obj)
{
Return Object. prototype. toString. call (obj) = "[object" + type + "]";
}
}
) (C );
}
Alert (is. Array ([]); // true
Alert (is. Date (new Date); // true
Alert (is. RegExp (/reg/ig); // true

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.