-
Notifications
You must be signed in to change notification settings - Fork 1
/
d-对象引用.html
62 lines (62 loc) · 1.06 KB
/
d-对象引用.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body></body>
<script type="text/javascript">
var a = b = {}
a = 3
console.log(b) //{}
var c = {
a: 3
}
var d = c
d = {} //开辟新的引用空间
console.log(c) //{a:3}
//按共享传递 call by sharing
var obj2 = {
x: 1
};
function foo2(o) {
o = 100;
}
foo2(obj2);
console.log(obj2.x); // 仍然是1, obj并未被修改为100.
var obj = {
x: 1
};
function foo(o) {
o.x = 3;
}
foo(obj);
console.log(obj.x); // 3, 被修改了!
var a = 100
function add(t) {
a = t + 100
}
add(100)
var c = 100
function addC(c) {
c = c + 100
}
addC(100)
console.log(c)
console.log(a)
function changeAgeAndReference(person) {
person.age = 25;
person = {
name: 'John',
age: 50
};
return person;
}
var personObj1 = {
name: 'Alex',
age: 30
};
var personObj2 = changeAgeAndReference(personObj1);
console.log(personObj1); // -> ? {name:"Alex",age:25}
console.log(personObj2); // -> ? {name:'John',age:50}
</script>
</html>