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 pathindex2.html
More file actions
111 lines (82 loc) · 2.45 KB
/
Copy pathindex2.html
File metadata and controls
111 lines (82 loc) · 2.45 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
<!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.html" target="_blank">Javascript面向对象编程(二):构造函数的继承</a>
<script>
function Animal() {
this.species = '动物';
}
// 构造函数绑定继承
function Cat1(name, color) {
Animal.apply(this, arguments);
this.name = name;
this.color = color;
}
var cat1_1 = new Cat1('chocola', 'brown');
// console.log(cat1_1);
// prototype模式继承
function Cat2(name, color) {
this.name = name;
this.color = color;
}
Cat2.prototype = new Animal();
Cat2.prototype.constructor = Cat2;
var cat2_1 = new Cat2('vanilla', 'white');
// console.log(cat2_1);
// 直接继承prototype
function Animal2() {}
Animal2.prototype.species = '动物';
function Cat3(name, color) {
this.name = name;
this.color = color;
}
Cat3.prototype = Animal2.prototype;
Cat3.prototype.constructor = Cat3;
var cat3_1 = new Cat3('vanilla', 'white');
// console.log(cat3_1);
// console.log(Animal2.prototype.constructor);
// 利用空对象作为中介
function Animal3() {}
Animal3.prototype.species = '动物';
function Cat4(name, color) {
this.name = name;
this.color = color;
}
function extend(Child, Parent) {
var F = function () {}
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
extend(Cat4, Animal3);
var cat4_1 = new Cat4('vanilla', 'white');
// console.log(cat4_1);
// console.log(Animal3.prototype.constructor);
// 拷贝继承
function Animal4() {}
Animal4.prototype.species = '动物';
function Cat5(name, color) {
this.name = name;
this.color = color;
}
function extend(Child, Parent) {
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p) {
c[i] = p[i];
}
c.uber = p;
}
extend(Cat5, Animal4);
var cat5_1 = new Cat5('vanilla', 'white');
// console.log(cat5_1);
</script>
</body>
</html>