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

77
Visualizações
Getter accessor is showing undefined on a JS Class

Let's say I have the following class...

Class Person {
   constructor(name, drives = false) {
      this.name = name;
      this._drives = drives;
   }

   get drives() {
      return this._drives;
   }

   set drives(drives) {
      this._drives = drives;
   }
}

Now let's say that I store a bunch of "Person" objects in localstorage. Now I want to loop through the the localstorage and find the "Person" that does not drive (I created 1 out of 5 who has _drives as TRUE). When I do this, they all come back as undefined...why?

const testFunction = () => {
for (let i = 0; i <= localStorage.length; i++) {
    const key = localStorage.key(i);
    const storedObj = JSON.parse(localStorage.getItem(key));


    if (!storedObj.drives === true) {
        console.log(storedObj.drives);
    }
  }
}

testFunction();

What's wrong here?

about 4 years ago · Juan Pablo Isaza
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