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

127
Visualizações
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 Respostas
Responde à pergunta

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 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