This JavaScript class doesn't set the attributes using the constructor. For some reason, they come out as undefined. I'm really confused, because if I take out the set/get methods it works, but with the set/get methods , name/genus return undefined.
class Animal {
constructor(genus, name){
this.name = name;
this.genus = genus;
}
// called when setting this.name (i.e. this.name = value)
set name(name){
console.log("you set name");
}
// Called when getting this.name
get name(){
console.log("you got name");
}
// Called when setting this.name (i.e. this.genus = value)
set genus(genus){
console.log("you set genus");
}
// called when getting this.genus
get genus(){
console.log("you got genus");
}
makeNoise(noise = ""){
console.log(noise);
}
// Class method
static _repr(){
return "Animal Class";
}
}
> let cat = new Animal("Feline", "Daisy");
you set name
you set genus
undefined
> cat
Animal {}
> cat.name
you got name
undefined
> cat.genus
you got genus
undefined
returnanything?