-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypeOf.js
58 lines (57 loc) · 1.37 KB
/
typeOf.js
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
/*
* typeOf({},['object','string']) == true;
*
* typeOf({},'object) == true;
*
* typeOf({}) == 'Object'
*
* typeOf(null, 'Object') == false;
*
* typeOf(function(){},'function') == true;
*
* typeOf(undefined, 'undefined') == true;
*
* typeOf(null,'null') == true;
*
* typeOf() == 'undefined';
*
* typeOf(NaN) == 'NaN';
*/
if (!typeOf) {
var typeOf = function(obj, type) {
/*
* if (obj == undefined || obj == null) { if (type == undefined || type ==
* null) { return true; } else { return false; } }
*/
var clas;
if (obj === undefined) {
clas = 'Undefined';
} else if (obj === null) {
clas = 'Null';
} else {
clas = Object.prototype.toString.call(obj).slice(8, - 1);
if (clas.toLowerCase() == 'number' && isNaN(obj)) {
clas = 'NaN';
}
}
// 分析type的类型 // analyse the type of the param named 'type'
if (type != undefined && type != null) {
if (typeOf(type) == 'Array') {
var length = type.length;
while (length-- > 0) {
if (typeOf(type[length], 'String') && clas.toLowerCase() == type[length].toLowerCase()) {
clas = true;
break;
}
}
delete length;
} else if (typeOf(type) == 'String') {
clas = clas.toLowerCase() == type.toLowerCase();
}
}
return clas;
delete clas;
delete arguments;
/**/
};
}