Home  >  Article  >  Web Front-end  >  Various methods of JavaScript isArray() function to determine object type_javascript skills

Various methods of JavaScript isArray() function to determine object type_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:18:581150browse
1) typeof operator
typeof is a unary operator, and the return result is a string describing the type of the operand. For example: "number", "string", "boolean", "object", "function", "undefined" (can be used to determine whether a variable exists).
But typeof has limited capabilities. It returns "object" for Date and RegExp types. For example:
Copy code The code is as follows:

typeof {}; // "object"
typeof []; // "object"
typeof new Date(); // "object"

So it is only useful when distinguishing between objects and primitive types. To distinguish one object type from another, other methods must be used. Such as: instanceof operator or constructor property of object.

2) instanceof operator.
The instanceof operator requires that the operand on the left is an object, and the operand on the right is the name or constructor of the object class. The instanceof operator returns true if object is an instance of a class or constructor. Returns false if object is not an instance of the specified class or function, or if object is null. Such as:

[] instanceof Array; // true
[] instanceof Object; // true
[] instanceof RegExp; // false
new Date instanceof Date; // true

So, you can use the instanceof operator to determine whether the object is an array type:

function isArray(arr)
{
return arr instanceof Array;
}

3) constructor attribute.
In JavaScript, each object has a constructor attribute, which refers to the constructor that initializes the object. It is often used to determine the type of unknown objects. For example, given a query value, use the typeof operator to determine whether it is a primitive value or an object. If it is an object, you can use the constructor attribute to determine its type. So the function to judge the array can also be written like this:

function isArray(arr)
{
return typeof arr == "object" && arr.constructor == Array;
}

In many cases, we can use the instanceof operator or the constructor property of the object to detect whether the object is an array. For example, many JavaScript frameworks use these two methods to determine whether an object is an array type.
But when detecting arrays in cross-frame pages, it will fail. The reason is that arrays created in different frames (iframes) do not share their prototype properties with each other. For example:
Copy code The code is as follows:

<script> <br>window.onload =function(){ <br>var iframe_arr=new window.frames[0].Array; <br>alert(iframe_arr instanceof Array); // false <br>alert(iframe_arr.constructor == Array); // false <br>} <br></script>




In Ajaxian I saw an accurate detection method, calling the toString() method across the prototype chain: Object.prototype.toString(). Can solve the above cross-framework problem.

When Object.prototype.toString(o) is executed, the following steps will be performed:
1) Get the class attribute of object o.
2) Connection string: "[object " result(1) "]"
3) Return result(2)

For example:

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

In this way, we You can write a robust function to determine whether an object is an array:
Copy the code The code is as follows:

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

This This method has been recognized by many foreign JavaScript masters, and this method will be used to detect arrays in the upcoming jQuery 1.3.

A maintainer of prototype.js wrote the following function, which is used to get the type name of an object
Copy code The 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(/^[objects(.* )]$/)[1];
};

Expand it to detect various object types:
Copy Code The 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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn