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:
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:CopyCodeThe 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 is used to obtain multipleJavascriptThis 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.
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