-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoop.html
More file actions
168 lines (166 loc) · 4.56 KB
/
Copy pathoop.html
File metadata and controls
168 lines (166 loc) · 4.56 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//类声明 构造函数
function Animal() {
this.name = 'Animal';
}
// ES6中class的声明
class Animal2 {
constructor() {
this.name = 'Animal2';
}
}
// 实例化
console.log('实例化',new Animal(), new Animal2());
// 借助构造函数
function Parent1() {
this.name = 'parent1';
}
//
Parent1.prototype.say = function () {
console.log('say');
}
//但是如果要继承原型对象上的方法是没办法继承的
function Child1() {
Parent1.call(this);
this.type = 'Child1';
}
console.log('借助构造函数',new Child1());
// 借助原型链实现继承
function Parent2() {
this.name = 'parent2';
}
function Child2() {
this.type = 'Child2';
}
Child2.prototype = new Parent2();//让child2的原型赋值为Parent2的实例
console.log('借助原型链',new Child2());
//组合方式
function Parent3() {
this.name = 'parent3';
this.play = [1,2,3]
}
function Child3() {
Parent3.call(this);
this.type = 'child3';
}
Child3.prototype = new Parent3();
var s3 = new Child3();
var s4 = new Child3();
s3.play.push(4);
console.log('组合式继承',s3,s4);
//组合继承优化1
function Parent4() {
this.name = 'parent3';
this.play = [1,2,3];
}
function Child4() {
Parent4.call(this);
this.type = 'child4';
}
Child4.prototype = Parent4.prototype;//做出了原型替换,相关的constructor也会改变
var s5 = new Child4();
var s6 = new Child4();
console.log('组合式改进1',s5,s6);
console.log('s5是否是Child4的子类',s5 instanceof Child4,'s5是否是Parent4的子类',s5 instanceof Parent4);
console.log('s5的constructor',s5.constructor);
//组合继承优化2
function Parent5() {
this.name = 'Parent5';
this.play = [1,2,3];
}
function Child5() {
Parent5.call(this);
this.type = 'Child5'
}
Child5.prototype = Object.create(Parent5.prototype);
//通过Object.create()创建一个新的对象,传入的原型对象是Parent.prototype
console.log('组合继承改进2',new Child5);
//改变constructor的指向
function Parent6() {
this.name = 'Parent6';
this.play = [1,2,3];
}
function Child6() {
Parent6.call(this);
this.type = 'Child6'
}
Child6.prototype = Object.create(Parent6.prototype);
Child6.prototype.constructor = Child6;
console.log('组合继承改进2-constructor',new Child6);
//原型式继承
function object_oop(o) {
function F() {
}
F.prototype = o;
return new F();
}
var person = {
name:"zhangjianan",
friends:["yueyue","red"]
};
var OnePerson = object_oop(person);
console.log('原型式继承',OnePerson);
OnePerson.name = "Goge";
console.log('原型式继承',OnePerson);
var TwoPerson = object_oop(person);
TwoPerson.friends.push("red");
console.log('原型式继承',OnePerson,TwoPerson);
//ES5原型式继承
var ThreePerson = Object.create(person,{
name: {
value:"XIXI"
}
})
console.log(ThreePerson);
var FourPerson = Object.create(ThreePerson,{
name:{
value:[1,2,3,4]
}
})
console.log('原型式继承',FourPerson);
//原型式继承-自我思想版
function list() {
this.name = 'List';
this.play = [1,2,3];
}
var my = Object.create(new list,
{
a:{value:"Grog"}
}
)
var my_count = Object.create(my,
{
num:{value:"000001"}
}
)
var you = Object.create(new list,
{
a:{value:"dog"}
}
)
console.log('自己想的原型式继承',my,you,my_count);
//new
var new2 = function (func) {
var o = Object.create(func.prototype);
var k = func.call(o);
if (typeof k === 'object') {
return k
}else{
return o
}
}
function new_todo() {
this.name = 'zhang';
}
var o6 = new2(new_todo);
console.log('自己制造new',o6)
</script>
</body>
</html>