Skip to content

Commit a2207bb

Browse files
committedDec 13, 2022
核心篇
1 parent d4f8edd commit a2207bb

File tree

20 files changed

+119
-0
lines changed

20 files changed

+119
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<script>
11+
var a = {
12+
a:1
13+
};
14+
console.log(a.a);
15+
</script>
16+
</body>
17+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var
2+
b1 = 1,
3+
b2 = b1;
4+
5+
b2 = 2;
6+
console.log(b1, b2);
7+
8+
var
9+
r1 = {
10+
a: 1,
11+
},
12+
r2 = r1;
13+
14+
r2.a = 2;
15+
console.log(r1.a, r2.a);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//key:字符串
2+
var person1 = {
3+
age: 28,
4+
hobby: "学习",
5+
son: {
6+
age: 3,
7+
hobby: "drink milk",
8+
friends: ['jack', 'lucy']
9+
}
10+
}
11+
12+
13+
// function clone1(obj) {
14+
// return obj;
15+
// }
16+
// var clonePerson = clone1(person1);
17+
// console.log(clonePerson);
18+
// console.log(clonePerson === person1);
19+
20+
21+
22+
//in会读原型链可枚举的属性
23+
//浅拷贝
24+
function clone2(obj) {
25+
var clonePerson = Array.isArray(obj) ? [] : {};
26+
for (var key in obj) {
27+
// 为什么不用clonePerson.key
28+
// 读对象的属性 1. person.age person[age]
29+
clonePerson[key] = obj[key];
30+
}
31+
return clonePerson;
32+
}
33+
var newPerson = clone2(person1)
34+
35+
newPerson.son.age = 100;
36+
console.log(person1.son.age);
37+
38+
39+
// var clonePerson = clone2(person1);
40+
// console.log(clonePerson);
41+
// console.log(clonePerson === person1);
42+
// clonePerson.son.age = 100;
43+
// console.log(clonePerson);
44+
// console.log(person1);
45+
46+
47+
//-------------------------深拷贝
48+
function clone3(obj) {
49+
if (typeof obj !== 'object' || obj === null) {
50+
return obj;
51+
}
52+
53+
var clone = Array.isArray(obj) ? [] : {};
54+
for (var key in obj) {
55+
if (typeof obj[key] === 'object') {
56+
//obj[key]; -> 新建 -> 新对象的地址
57+
clone[key] = clone3(obj[key]);
58+
} else {
59+
clone[key] = obj[key];
60+
}
61+
}
62+
63+
return clone;
64+
}
65+
66+
// var clonePerson = clone3(person1);
67+
// console.log(clonePerson === person1);
68+
// clonePerson.son.age = 100;
69+
// clonePerson.son.friends = ['tom'];
70+
// console.log(clonePerson);
71+
// console.log(person1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//js: 值-->读stack内存里的值
2+
function change1(arg) {
3+
arg = 200;
4+
}
5+
6+
function change2(arg) {
7+
arg.a = 200;
8+
}
9+
10+
var foo = { a: 2, b: { x: 1 } };
11+
console.log(foo);
12+
foo = {};//手动释放
13+
14+
15+
16+
// change1(bar);
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.