これは私の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機能があります。私は 2 匹の異なる猫 (cat1とcat2) を作成しました。それぞれの猫には独自の音がありますが、それらの音を印刷すると次のようになります。
MIAAAAUUUUUU MIAAAAUUUUUU
音は音をcat2上書きしますcat1。私はこれを望んでいません。
私は取得したい:
MIAO MIAAAAUUUUUU
誰でも私を助けることができますか?