create

说明

模拟实现 Object.create()

源码

用法

create.test.ts
1
import create from "../../utility/object/create";
2
3
export interface IStaticObject {
4
[key: string]: any;
5
};
6
describe('create tests...', () => {
7
test('The method `create()` should return the correct value while receiving a plain object', () => {
8
const received: IStaticObject[] = [
9
{
10
prototype: {
11
name: 'duanzhaoyang',
12
age: 22,
13
},
14
props: {},
15
},
16
{
17
prototype: {},
18
props: {
19
skill: 'running',
20
},
21
},
22
];
23
const expected: IStaticObject[] = [
24
{},
25
{
26
skill: 'running',
27
},
28
];
29
30
received.forEach((v, i) => {
31
const result = create(v.prototype, v.props);
32
33
for (const key in result) {
34
if (Object.prototype.hasOwnProperty.call(result, key)) {
35
const value = result[key];
36
37
expect(expected[i].hasOwnProperty(key)).toBeTruthy();
38
expect(expected[i][key]).toBe(value);
39
}
40
}
41
});
42
});
43
test('The method `create()` should return the correct value while receiving `null`', () => {
44
const received: IStaticObject[] = [
45
{
46
prototype: null,
47
props: {},
48
},
49
{
50
prototype: null,
51
props: {
52
a: 1,
53
b: 2,
54
},
55
},
56
];
57
const expected: IStaticObject[] = [
58
{},
59
{
60
a: 1,
61
b: 2,
62
},
63
];
64
65
received.forEach((v, i) => {
66
const result = create(v.prototype, v.props);
67
68
for (const key in result) {
69
if (Object.prototype.hasOwnProperty.call(result, key)) {
70
const value = result[key];
71
72
expect(expected[i].hasOwnProperty(key)).toBeTruthy();
73
expect(expected[i][key]).toBe(value);
74
}
75
}
76
});
77
});
78
});
Copied!
最近更新 11mo ago
复制链接