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
|
function Animal(kingdom,family){
//使用建構函式建立[Animal]原型
this.kingdom = kingdom;
this.family = family;
}
function Dog(name,color,size){
Animal.call(this,"動物界","犬科");
//讓[Dog]原型物件,傳入[Animal]原型裡,並帶兩個值到[Animal]參數裡
this.name = name;
this.color = color;
this.size = size;
}
Dog.prototype = Object.create(Animal.prototype);
//讓[Dog]原型,繼承在[Animal]原型下
Animal.prototype.move = function(){
//在[Animal]原型裡函式,新增[move]屬性
return this.name + "移動";
}
var Bibi = new Dog("比比","棕色","小型");
Dog.prototype.bark = function(){
return this.name + "吠叫";
}
Dog.prototype.constructor = Dog;
//將原本[狗]的函式加回到[狗]原型
console.log(Bibi.kingdom,Bibi.family); //"動物界" //"犬科"
console.log(Bibi.move()); //比比移動
|