Home > Article > Web Front-end > Summary of how JavaScript uses operators and properties to determine object type
The operators used to detect object types in JavaScript include: typeof, instanceof, and the constructor attribute of the object:
1) typeof operator typeof is a unary operator, 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 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. 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 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 value to be determined, 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 determine 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:
<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>
I 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) Obtain 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 can write a robust method to determine whether the object is Array function:
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 jQuery 1.3 . A maintainer of prototype.js wrote the following function, which is used to obtain the type name of an object
/** * 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]; };
Extend it 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
The above is the detailed content of Summary of how JavaScript uses operators and properties to determine object type. For more information, please follow other related articles on the PHP Chinese website!