This repository was archived by the owner on Sep 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex3.html
More file actions
71 lines (60 loc) · 1.64 KB
/
Copy pathindex3.html
File metadata and controls
71 lines (60 loc) · 1.64 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Javascript面向对象编程(三):非构造函数的继承</title>
</head>
<body>
<a href="http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html">Javascript面向对象编程(三):非构造函数的继承</a>
<script>
var Chinese = {
nation: '中国'
}
var Doctor = {
career: '医生'
}
// object()方法
function object(o) {
function F() {};
F.prototype = o;
return new F();
}
var Doctor1 = object(Chinese);
Doctor1.career = '医生';
// console.log(Doctor1);
// 浅拷贝
function extendCopy(p) {
var c = {};
for (var i in p) {
c[i] = p[i];
}
c.uber = p;
return c;
}
var Doctor2 = extendCopy(Chinese);
Doctor2.career = '医生';
// console.log(Doctor2);
// 深拷贝
function deepCopy(parent, child) {
var child = child || {};
for (var i in parent) {
if (parent[i] === null) {
child[i] = null;
} else if (typeof parent[i] === 'object') {
child[i] = (parent[i].constructor === Array) ? [] : {};
deepCopy(parent[i], child[i]);
} else {
child[i] = parent[i];
}
}
return child;
}
Chinese.birthPlaces = ['北京', '上海', '香港'];
var Doctor3 = deepCopy(Chinese);
Doctor3.birthPlaces.push('厦门');
console.log(Doctor3);
</script>
</body>
</html>