Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
JS class attributes return undefined with constructor/set get methods

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
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When you do this.name = name in the constructor, you're invoking the setter. If you want to save the value passed in the constructor arguments, and also save the value when the properties are assigned to outside (eg animal.name = 'foo'), you'll need to

  • Use a different property name to internally store the data - something other than name, which is the same property name as the getter/setter
  • Fix the getters and setters to get and set that internal property

class Animal {
  constructor(genus, name) {
    this._name = name;
    this._genus = genus;
  }
  set name(name) {
    console.log('setter invoked');
    this._name = name;
  }
  get name() {
    console.log('getter invoked');
    return this._name;
  }
  set genus(genus) {
    this._genus = genus;
  }
  get genus() {
    return this._genus;
  }
}

const a = new Animal('gen1', 'name1');
console.log(a.name);
a.name = 'name2';
console.log(a.name);

about 4 years ago · Juan Pablo Isaza Relatório

0

Your get methods need to actually return the values. You're receiving undefined because all the getters are doing is console logging.

For instance:

get genus(){
    console.log("you got genus");
    return this.genus;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda