这是我的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
谁能帮助我?
非常感谢您的时间:)现在,它的工作原理! – Fabio