forked from YvetteLau/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepCopy.test.js
More file actions
125 lines (110 loc) · 3.59 KB
/
deepCopy.test.js
File metadata and controls
125 lines (110 loc) · 3.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { deepCopy, deepClone } from '../src/deepCopy';
import { cloneDeep } from '../node_modules/lodash/lodash';
test('deepCopy 普通对象', () => {
let source = {
name: 'Yvette',
age: 20,
time: new Date(),
hobbies: ['reading', 'photography'],
sayHi: function () {
return 'Hi';
}
};
let target = deepCopy(source);
target.name = 'Tom';
target.hobbies.push('coding');
//深拷贝意味着 target 和 source 是完全独立的,不会相互影响
expect(source.name).toBe('Yvette');
expect(source.hobbies).toEqual(['reading', 'photography']);
expect(target.sayHi()).toBe('Hi'); //函数正常拷贝
expect(target.time instanceof Date).toBe(true);//Date数据类型正常
});
test('deepCopy 原型链上属性拷贝', () => {
function SupSuper() { }
SupSuper.prototype.from = 'China';
function Super() { }
Super.prototype = new SupSuper();
Super.prototype.location = 'NanJing';
function Child(name, age, hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
Child.prototype = new Super();
let obj = new Child('Yvette', 18, ['reading', 'photography']);
let obj2 = deepCopy(obj);
expect(obj2).toEqual({
name: 'Yvette',
age: 18,
hobbies: ['reading', 'photography']
});
/**构造函数原型上的属性可以拷贝 */
expect(obj2.from).toBe('China');
/**Super原型上的属性获取不到,返回Undefined */
expect(obj2.location).toBeUndefined();
/**obj是能从原型上获取到location属性的 */
expect(obj.location).toBe('NanJing');
});
test('deepCopy 不支持循环引用', () => {
let obj = {
name: 'Yvette',
age: 20
}
obj.info = obj;
//抛出异常
// expect(deepCopy(obj)).toThrow();
});
test('deepClone 普通对象', () => {
let source = {
name: 'Yvette',
age: 20,
time: new Date(),
hobbies: ['reading', 'photography'],
sayHi: function () {
return 'Hi';
}
};
let target = deepClone(source);
target.name = 'Tom';
target.hobbies.push('coding');
//深拷贝意味着 target 和 source 是完全独立的,不会相互影响
expect(source.name).toBe('Yvette');
expect(source.hobbies).toEqual(['reading', 'photography']);
expect(target.sayHi()).toBe('Hi'); //函数正常拷贝
expect(target.time instanceof Date).toBe(true);//Date数据类型正常
});
test('deepClone 原型链上属性拷贝', () => {
function SupSuper() { }
SupSuper.prototype.from = 'China';
function Super() { }
Super.prototype = new SupSuper();
Super.prototype.location = 'NanJing';
function Child(name, age, hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
Child.prototype = new Super();
let obj = new Child('Yvette', 18, ['reading', 'photography']);
let obj2 = deepClone(obj);
expect(obj2).toEqual({
name: 'Yvette',
age: 18,
hobbies: ['reading', 'photography']
});
/**构造函数原型上的属性可以拷贝 */
expect(obj2.from).toBe('China');
/**Super原型上的属性获取不到,返回Undefined */
expect(obj2.location).toBeUndefined();
/**obj是能从原型上获取到location属性的 */
expect(obj.location).toBe('NanJing');
});
test('deepClone 循环引用', () => {
let obj = {
name: 'Yvette',
age: 20
}
obj.info = obj;
//和 lodash 的 cloneDeep(obj) 结果一致
expect(deepClone(obj)).toEqual(cloneDeep(obj));
});