Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
84 lines (54 loc) · 934 Bytes

File metadata and controls

84 lines (54 loc) · 934 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*正常的情况*/
var a = 5;
var b = a;
b += 3;
alert(b); //8
alert(a); //5
//对象和函数都是引用的关系
var a = [1,2,3];
var b = a;
b.push(4);
alert(b); //1,2,3,4
alert(a); //1,2,3,4*/
//数组重新写
var a = [1,2,3];
var b = a;
b = [1,2,3,4];
alert(b); //1,2,3,4
alert(a); //1,2,3
/*对象赋值*/
var obj = {
a : 10
};
var obj2 = obj;
obj2.a = 20;
alert(obj.a); //20*/
//赋值解决方案
var obj = {
a : 10
};
function copy(obj){ //浅拷贝
var newObj = {};
for(var attr in obj){
newObj[attr] = obj[attr];
}
return newObj;
}
var obj2 = copy(obj);
obj2.a = 20;
alert(obj.a); //10
//解决方案二
function deepCopy(obj){ //深拷贝
if(typeof obj != 'object'){
console.trace();
return obj;
}
var newObj = {};
for(var attr in obj){
newObj[attr] = deepCopy(obj[attr]);
}
return newObj;
}
var obj2 = deepCopy(obj);
obj2.a.b = 20;
alert(obj.a.b); //10
Morty Proxy This is a proxified and sanitized view of the page, visit original site.