_instanceOf

说明

模拟实现原生的instanceOf

源码

用法

_instanceOf.test.ts
1
import { _instanceOf } from "../../utility/others/_instanceOf";
2
3
describe('_instanceOf tests...', () => {
4
5
test('_instanceOf should return the correct result', () => {
6
interface PersonConstructor {
7
new(): PersonInterface;
8
};
9
interface PersonInterface {
10
};
11
12
class Person implements PersonInterface {
13
constructor() {
14
}
15
}
16
17
const received: Array<{
18
left: any,
19
right: any,
20
}> = [
21
{
22
left: new Person(),
23
right: Person,
24
},
25
{
26
left: {},
27
right: Object,
28
},
29
{
30
left: [],
31
right: Array,
32
},
33
{
34
left: [],
35
right: Object,
36
},
37
{
38
left: 2,
39
right: 8,
40
},
41
{
42
left: 'ddzy',
43
right: {},
44
},
45
{
46
left: {},
47
right: null,
48
},
49
];
50
const expected = [true, true, true, true, false, false, false,];
51
52
received.forEach((v, i) => {
53
const result = _instanceOf(v.left, v.right);
54
55
expect(result).toBe(expected[i]);
56
});
57
});
58
59
});
Copied!
最近更新 1yr ago
复制链接