/**
* param: o represents the detected value
* return: Returns the string "undefined", "number", "boolean", "string", "function", "regexp", "array", "date", "error", "object" or "null"
*/
function typeOf(o){
var _toString = Object.prototype.toString; //Get the toString() method reference of the object
//List basic data types and built-in object types. You can further supplement 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"); //By converting the value to a string, and then matching whether the returned string contains a specific Characters are detected
}
//Application example:
var a = Math.abs;
alert(typeOf(a)); //return string "function"
The code is very simple, and the instructions are all in the comments. I won’t go into too much nonsense here. Friends who have the same needs can refer to it for their own reference