I have a dialog in my html with id="divMyDialog1"
. This is associated with a javascript class with a name MyDialog1
. (These names are just for illustration only.)
MyDialog1 = function(divStr){
this.divStr = divStr;
...
}
I have loads of these dialogs and associated classes. What I would like to do is set up all these different string id
s and associate them with their dialog class. Some Dialog Classes would have multiple instances but with different divStr
.
But how would this be done?
I want something on the lines:
let dialogARR = [];
[{divStr: "divMyDialog1", className: MyDialog1},
{divStr: "divSomethingElse", className: DialogWibble},
{divStr: "divPingPong", className: DialogWobble}].forEach(o => dialogARR.push(new o.className(o.divStr)));
2
Answers
You could just have:
And then initialize dialogs like this into the array:
That’s how I would go about it, please correct me if I’m wrong.
Loop through the array, extracting the properties from each object and then calling the class constructor function with
new
.If you need the instances, you can use
.map()
instead of.forEach()
, and it will return an array of the instances.