Home > Article > Web Front-end > Summary of Several Methods for Determining Object Types in JavaScript_Basic Knowledge
We know that the operators for detecting object types in JavaScript are: typeof, instanceof, and the constructor attribute of the object:
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). However, 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 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 desired 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 fails. The reason is that arrays created in different frames (iframes) do not share their prototype properties with each other. For example:
Object.prototype.toString.call([]); // Returns "[object Array]"
Object.prototype.toString.call(/reg/ig); // Returns "[object RegExp]"
In this way, we can write a robust function to determine whether the object is an array:
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