How to return an object from a dynamic reference in JavaScript?

2

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.     

asked by anonymous 30.09.2016 / 21:13

4 answers

2

The easiest is to use an array:

// Define class

function Test(num) {

    this.msg = "Message";   
    this.num = num;

}

var objetos = [];
objetos.push( new Test(1) );
objetos.push( new Test(2) );
objetos.push( new Test(3) );

for (var i = 0; i < 3; i++) { 
    // ^ IMPORTANTE, faltava o var

    alert( objetos[i].msg + objetos[i].num );
}
    
30.09.2016 / 21:22
1

You can use the global object to access global variables dynamically. In the browser the global object is window (you can use this also, which references the same global object as window points).

// 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++) { 
    // Dinamic reference
    console.log( window["objTest_" + i].msg + window["objTest_" + i].num );
}

If these objects are declared with var within functions the variables will be associated with what is called the Activation Object . But unlike the global object, there is no way to access it, and consequently there is no way to access the variables associated with it.

In this case you have the option to save the objects in an array, as suggested in one of the answers, or use the eval function. Assuming your code is inside an anonymous function:

(function() {
  // 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++) { 
    // Dinamic reference
    console.log( eval("objTest_" + i).msg + eval("objTest_" + i).num );
  }
})()

eval will execute the string you pass as parameter. But be careful! The eval function should be used in the latter case and very cautiously. Using eval is dangerous because, depending on how you generate the string to be executed, you may end up injecting some malicious script into the user's machine.

References:

04.10.2016 / 17:38
0

I discovered a way to dynamically call a javascript object by its id:

window["objTest_" + 1]
    
04.10.2016 / 14:16
0

You can use

  

eval ()

In practice eval () runs a string as if it were a javascript code.

var x = 10;
var y = 20;
var a = eval("x * y") + "<br>";
var b = eval("2 + 2") + "<br>";
var c = eval("x + 17") + "<br>";

var res = a + b + c;

Result

200
4
27

link

Example using in objects.

var obj = { a: 20, b: 30 };
var propname = getPropName();  //returna "a" ou "b"

eval( "var result = obj." + propname );

link

    
04.10.2016 / 14:59