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. Such as:
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 attribute 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:
<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>
Saw an accurate detection method on Ajaxian, 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:
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 to get the type name of an object
function __getClass(object)
{
return Object.prototype.toString .call(object).match(/^[objects(.*)]$/)[1];
};
Extended to detect various object types:
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