这是我的 JavaScript 代码:
function animal(){
var animal_sound;
this.animal = function(sound){
animal_sound = sound;
}
this.returnSound = function(){
return animal_sound;
}
}
function cat(){
this.cat = function(sound){
this.animal(sound);
}
}
cat.prototype = new animal()
cat.prototype.constructor = cat;
//Create the first cat
var cat1 = new cat();
cat1.cat('MIAO');//the sound of the first cat
//Create the second cat
var cat2 = new cat();
cat2.cat('MIAAAAUUUUUU');//the sound of the second cat
alert(cat1.returnSound()+' '+cat2.returnSound());
只是我有cat
扩展功能的animal
功能。比我创造了两只不同的猫(cat1
和cat2
)。每只猫都有自己的声音,但是当我打印它们的声音时,我得到:
MIAAAAUUUUUU MIAAAAUUUUUU
cat2
声音会覆盖cat1
声音,我不想要这个。
我想获得:
MIAO MIAAAAUUUUUU
谁能帮我?