The code copy is as follows:
/**
* param: o represents the detected value
* return: Return the string "undefined", "number", "boolean", "string", "function", "regexp", "array", "date", "error", "object" or "null"
*/
function typeOf(o){
var _toString = Object.prototype.toString; //Get the object's toString() method reference
// List the basic data types and built-in object types, and 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"); //Detection by converting the value to a string and then matching the return string contains a specific character
}
//Application example:
var a = Math.abs;
alert(typeOf(a)); //Return the string "function"
The code is very simple, and the instructions are all in the comments, so I won’t talk much nonsense here. If you have the same needs, please refer to it yourself.