-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheckType.js
More file actions
60 lines (56 loc) · 1.81 KB
/
Copy pathcheckType.js
File metadata and controls
60 lines (56 loc) · 1.81 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var typeName = {
'[object Function]': 'function',
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Object]': 'object',
'[object RegExp]': 'regExp',
'[object Array]': 'array',
'[object Error]': 'error',
'[object Date]' : 'date'
};
var toStringFn = Object.prototype.toString; //获取到对象的toString方法
function checkType(obj){
if( obj == null ){
return String( obj ); //js自带的的String方法,用于检测null和undefined
}
//safari5及之前版本,Chrome7, typeof RegExp返回的是function
return typeof obj ==='object' || typeof obj === "function" ?
typeName[toStringFn.call(obj)]:typeof obj;
}
// --- test ---
var obj = {};
var str = 'string';
var num = 123;
var bool = true;
var arr = [];
var reg = /a-z/;
var fn = function(){};
var obj_null = null;
var undf = undefined;
var obj2 = new Object();
var obj_str = new String('string');
var obj_num = new Number();
var obj_bool = new Boolean();
var obj_arr = new Array();
var obj_reg = new RegExp();
var obj_fn = new Function();
var obj_date = new Date();
console.log(checkType(obj)); //object
console.log(checkType(str)); //string
console.log(checkType(num)); //number
console.log(checkType(bool)); //boolean
console.log(checkType(arr)); //array
console.log(checkType(reg)); //regExp
console.log(checkType(fn)); //function
console.log(checkType(obj_null)); //null
console.log(checkType(undf)); //undefined
console.log('-----------------------分界线');
console.log(checkType(obj2)); //object
console.log(checkType(obj_str)); //string
console.log(checkType(obj_num)); //number
console.log(checkType(obj_bool)); //boolean
console.log(checkType(obj_arr)); //array
console.log(checkType(obj_reg)); //regExp
console.log(checkType(obj_fn)); //function
console.log(checkType(obj_date)); //date