function Mammal(name) {
this.name = name;
this.offspring = [];
}
Mammal.prototype.haveABaby = function() {
var newBaby = new Mammal('Baby ' + this.name);
this.offspring.push(newBaby);
return newBaby;
}
Mammal.prototype.toString = function() {return '[Mammal "' + this.name + '"]';}
Cat.prototype = new Mammal();
Cat.prototype.constructor = Cat;
function Cat(name) {this.name = name;}
Cat.prototype.toString = function() {
return '[Cat " ' + this.name + '"]';
}
var someAnimal = new Mammal('Mr. Biggles');
var myPet = new Cat('Felix');
myPet.haveABaby();
javascript实现继承
最新推荐文章于 2022-04-26 13:20:17 发布