Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

76
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda