0

Is it possible to initialize new classes through a loop with placeholders? Sorry if I use the words incorrectly.

class Data{
  constructor(id, name,car){
    this.id = id;
    this.name = name;
    this.car = car;
  }
}

function initializeClasses(){
  var characters = ["A","B","C","D","E"];
  var cars = ["Audi", "BMW", "Porsche", "", ""]; // Includes empty slots.
  
  for (i = 0; i < characters.length; i++){
    var str = String(i+characters[i]); // To make a new name, I want to know that because I need that for something.   
    let ${str} = new Data(i, characters[i], cars[i]); // The placeholder.
  }
}
Noob
  • 1
  • 1
  • 1
    No good way. Use an object or Map instead – CertainPerformance Feb 22 '21 at 19:06
  • 1
    Create an array of `Data` instances? – adiga Feb 22 '21 at 19:07
  • Yes, it's possible, `window[str] = new Data(...)`. It's a terrible idea though. – Galunid Feb 22 '21 at 19:07
  • Thank you @CertainPerformance, Adiga and Galunid I want to use a 3D Matrix collecting as adjustable as a Class including formulas calculating new data which also collect data through loops initializing entering or adding new rows, columns and beams. – Noob Feb 22 '21 at 22:31

0 Answers0