1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| //参数:o表示监测的值
/*返回值:返回字符串“undefined”, "number", "boolean", "string" ,"function", "regexp" ,"array", "date", "error", "object" 或 "null" */
function typeOf(o){
var _toString = Object.prototype.toString;
// 获取对象的toString()方法引用
//列举基本数据类型和内置对象类型,可以进一步补充该数组的监测数据类型范围
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");
}
//测试
console.log(typeOf(undefined));// "undefined"
console.log(typeOf(2));// "number"
console.log(typeOf(true));// "boolean"
console.log(typeOf("hello"));// "string"
console.log(typeOf(function(){}));// "function"
console.log(typeOf(/hello/));// "regexp"
console.log(typeOf([1,2,3]));// "array"
console.log(typeOf(new Date()));// "date"
console.log(typeOf(new Error()));// "error"
console.log(typeOf({}));// "object"
console.log(typeOf(null));// "null"
|