-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathgetType.js
48 lines (39 loc) · 1.28 KB
/
getType.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
// 手写类型判断函数
function getType(value) {
// 判断数据是null的情况
if(value === null) {
return 'object';
}
// 判断数据是引用类型的情况
if(typeof value === 'object') {
let valueClass = Object.prototype.toString.call(value),
type = valueClass.split(" ")[1].split("");
console.log('v', valueClass)
console.log('v split', valueClass.split(" "))
console.log('v split 1', valueClass.split(" ")[1])
console.log('v split 1 split', valueClass.split(" ")[1].split(""))
type.pop();
console.log('v pop', type, type.join("").toLowerCase())
return type.join("").toLowerCase();
} else {
// 判断数据是基本数据类型的情况和函数的情况
return typeof value;
}
}
console.log(typeof null); // object
console.log(typeof undefined); // undefined
console.log(typeof [1,2,3]); // object
// expected output: "number"
// console.log(typeof 'blubber');
// expected output: "string"
// console.log(typeof true);
// expected output: "boolean"
// console.log(typeof undeclaredVariable);
// expected output: "undefined"
// console.log(getType(12))
// console.log(getType(null))
// console.log(getType(undefined))
console.log(getType('blubber'))
console.log(getType(true))
console.log(getType({ age: 12, job: 'it' }))
console.log(getType([1,2,3])) // array