-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path024_oop8.htm
More file actions
93 lines (76 loc) · 2.13 KB
/
024_oop8.htm
File metadata and controls
93 lines (76 loc) · 2.13 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8>
// 原型继承
//原型继承的特点:即继承了父类的模版,又继承了 父类的原型对象
/*
// 父类
function Person(name, age){
this.name = name ;
this.age = age ;
}
// 父类的原型对象属性
Person.prototype.id = 10 ;
// 子类
function Boy(sex){
this.sex = sex ;
}
//继承已经实现了
Boy.prototype = new Person('z3');
var b = new Boy();
alert(b.name);
alert(b.id);
*/
// 类继承(只继承模版,不继承原型对象) (借用构造函数的方式继承)
/*
// 父类
function Person(name, age){
this.name = name ;
this.age = age ;
}
// 父类的原型对象属性
Person.prototype.id = 10 ;
// 子类
function Boy(name , age , sex){
// call apply
Person.call(this,name,age);
this.sex = sex ;
}
var b = new Boy('张三' , 20 , '男');
//alert(b.name);
//alert(b.age);
//alert(b.sex);
//alert(b.id); //父类的原型对象并没有继承
*/
// 原型继承+借用构造函数继承 = 混合继承
/*
// 父类
function Person(name, age){
this.name = name ;
this.age = age ;
}
// 父类的原型对象属性
Person.prototype.id = 10 ;
Person.prototype.sayName = function(){alert(this.name);};
// 子类
function Boy(name , age , sex){
// call apply
Person.call(this,name,age); // 1 借用构造函数继承 继承父类的模版
this.sex = sex ;
}
// 2 原型继承
// 只剩下 父类的实例 和 父类的原型对象的关系了
Boy.prototype = new Person(); //继承父类的原型对象
var b = new Boy('李四' , 20 , '男');
alert(b.name);
alert(b.sex);
b.sayName();
*/
</script>
</head>
<body>
</body>
</html>