I need to create JavaScript code that returns an object already created with a dynamic reference by its id / id.
Basically this:
// Define class
function Test(num) {
this.msg = "Message";
this.num = num;
}
// Create objects
var objTest_1 = new Test(1);
var objTest_2 = new Test(2);
var objTest_3 = new Test(3);
// Call objects in loop
for (i = 1; i <= 3; i++) {
/*
// Static reference
alert( objTest_1.msg + objTest_1.num );
//or
alert( Object(objTest_1).msg + Object(objTest_1).num );
*/
// Dinamic reference
alert( Object("objTest_" + i).msg + Object("objTest_" + i).num ); // Return error "NaN"
}
Ps: In this example I just look for some "property" of the object, but in my actual code I really need to return an existing object to call a specific method it contains.