-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdemo5.html
43 lines (39 loc) · 970 Bytes
/
demo5.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>寄生组合继承</title>
</head>
<body></body>
</html>
<script>
/**
* @method extend
* @param {function} r the object to modify.
* @param {function} s the object to inherit.
*/
function Extend(r, s) {
var sp = s.prototype;
var rp = Object.create(sp);
rp.constructor = r;
r.prototype = rp;
}
function KFC(food) {
this.food = food;
}
KFC.prototype.getFood = function () {
console.log(this.food)
}
function Mcdonalds(food, newFood) {
KFC.call(this, food);
this.newFood = newFood;
}
Extend(Mcdonalds, KFC);
Mcdonalds.prototype.getNewFood = function () {
console.log(this.newFood)
}
var dog = new Mcdonalds("老北京鸡肉卷", "5G炸鸡");
dog.getFood(); // "老北京鸡肉卷"
dog.getNewFood(); // "5G炸鸡"
</script>