-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path33.组合式继承.html
46 lines (41 loc) · 1.55 KB
/
33.组合式继承.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
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>考查继承</title>
</head>
<body>
<script>
// 题目一:用js写一个继承实例
function Animal(color){
this.color = color; //因为每个动物有各自的颜色,new出实例时要针对性的传参进来
// 要么就这么写,要么就用下面的写法。如果这样写不能直接用对象字面量来写,因为这样会切断实例与构造函数的指向。但是放在下面可以因为,放下面是先执行修改指向在创建实例。
if(typeof this.eat!= "function"){
Animal.prototype.eat=function () {
console.log("不管是什么动物都有吃东西");
};
console.log("梦");
}
}
// Animal.prototype = {//实例共用的方法才放在这里,若放构造方法,每个实例都创建一遍浪费资源
// constructor:Animal,
// eat:function(){
// console.log("不管是什么动物都有吃东西");
// }
// }
function Dog(color,shuangYanPi){
this.shuangYanPi = shuangYanPi;
Animal.call(this,color);
}
Dog.prototype = new Animal();
var boyFriend = new Dog("花的",true);
boyFriend.eat();//
var animal = new Animal();
console.log(boyFriend.prototype);
// console.log(Dog.prototype.__proto__ == Animal.prototype);
// console.log(boyFriend.__proto__ == Dog.prototype);
// console.log(boyFriend.__proto__ == Dog.prototype);
// console.log(boyFriend.shuangYanPi);
</script>
</body>
</html>