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();
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)