This is my JavaScript code:
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());
Simply I have the cat
function that extend the animal
function. Than I have created two different cats (cat1
and cat2
). Each cat has the own sound but when I print their sounds I obtain:
MIAAAAUUUUUU MIAAAAUUUUUU
The cat2
sound overwrite the cat1
sound and I would not want this.
I would like to obtain:
MIAO MIAAAAUUUUUU
Can anyone help me?