JavaScript data type detection code sharing, javascript Data Type
Copy codeThe Code is as follows:
/**
* Param: o indicates the detected value.
* Return: returns the "undefined", "number", "boolean", "string", "function", "regexp", "array", "date", "error", and "object" strings. "or" null"
*/
Function typeOf (o ){
Var _ toString = Object. prototype. toString; // obtain the toString () method reference of the Object
// Lists the basic data types and built-in object types. You can also add the detection data type range of the array.
Var _ type = {
"Undefined": "undefined ",
"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"); // checks whether the returned string contains specific characters by converting the value to a string.
}
// Application example:
Var a = Math. abs;
Alert (typeOf (a); // returns the string "function"
The code is very simple. The instructions are all in comments. There is not much nonsense here. If you have the same requirements, refer to them.