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

162
Visualizações
Is it possible to create new objects in runtime in Javascript?

From my colloquial understanding of creating new objects from classes in Javascript one can do the following line of code:

const exampleVariable = new exampleClass("ExampleArguement");

I apologize for this being a novice question but is it possible to create new objects within the execution of the program? Sort of "automating" the creation of objects in my code.

Let's say I need to create 100 objects.

I could technically write 100 lines initializing variables and their respective objects but I am 100% sure there is a more efficient way of doing this.

I tried looking up a solution but I couldn't quite get the phrasing right so I would appreciate it if someone pointed me in the right direction.

Many thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use loops for that.

For example:

class SomeClass{
    constructor(id){
        this.id = id;
    }
}

const objects = [];
const amountOfObjects = 100;
// create objects
for (var i = 0; i < amountOfObjects; i++){
  const object = new SomeClass(i + 1);
  objects.push(object);
}
 
// iterate over the array of objects created and log their id
for (var i = 0; i < objects.length; i++){
  console.log(objects[i].id);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create an object in run-time. Just with:

const myObject = {};

Or you can use the constructor if you're more used with classes:

const myObject = new Object();

Finally, answering your question, in run-time you can create 100 objects, for instance, inside of an array or another structure:

const myArray = [];
for (let index = 0; index < 100; index++) {
  const myObject = { index };
  myArray.push(myObject)
}

With this code you will have { index: 0 } inside of myArray[0]. Then, you have 100 possible variables inside the whole array.

Hope it helps.

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