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

128
Vistas
How to initialize a class variable as an empty array in JavaScript?

I essentially just want to assign the default value of a class variable to an empty array.

I've tried the following, but did not have any luck:

class Test {
    constructor(myArray = []) {
        this.myArray = myArray;
    }
}

Obviously this doesn't work, as when I try to print Test.myArray, undefined is returned rather than an empty array.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are trying to access Prototype, your class is initialized correctly, just do:

class Test {
    constructor(myArray = []) {
        this.myArray = myArray;
    }
}

const t = new Test();

console.log(t.myArray)

new keyword means, that you are creating a new instance of the Test class.

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