1) when detecting JavaScript types, the typeof operator is usually used, and a string that identifies the number of operations is returned. For any variable, the return result using the typeof operator is of only 6 types.
1. Number 2, string 3, Boolean 4, object 5, function 6, and undefined. However, when you try to use typeof to detect null values, the returned object is not suitable. You may think that null should be of no type, therefore, most people are doing a method to determine the null type.
function type(oo){ return (oo ==null) ? "null":(typeof oo);}
2) but typeof cannot detect complex data types, such as expression objects, date objects, and mathematical objects.
It can only return one object type, and does not know the specific date or something,
In JavaScript, the constructor attribute is used to determine the data type.
VaR value = underfined;
Alert (value. constructor );
However, the direct numeric value cannot be used in this way. You need to add a parentheses. In this case, the parentheses operator can directly convert the numeric value into an object, a bit like the integer and INT types in Java,
Alert (30). constructor)
3) The tostring () method can also be used to detect object types and is the most accurate. Generally, the object is converted into a string, then, check whether the string contains the standard characters specific to the array,
The format of the returned string is as follows:
[Object calss]
If the object type is generic, the class indicates the internal type of the object. The name of the internal type corresponds to the constructor name of the object. For example, the class of the array object is array, class "Date" of the date object"
The specific statement is as follows,
var date = new Date(); alert(typeof date); var m=Object.prototype.toString; alert(m.apply(date));
Provides a common type detection code
Function Type (o ){
VaR _ tostring = object. Prototype. tostring;
VaR _ type = {
"Underfined": "underfined ",
"Number": "Number ",
"Boolean": "Boolean ",
"String": "string ",
''[Object function]": "function ",
''[Object Regexp]": "Regexp ",
''[Object array]": "array ",
''[Object date]": "date ",
''[Object Error]": "error"
}
Return _ type [typeof o] | _ type [_ tostring. Call (o)] | (O? "Object": "null ")
}