0

I need to do this Ps: "a" and "b" are just variables for the example, i want to send this numbers to another variable with a button , and with this "new variable" use ${} to call the correct object from my class

   class MyClass(){
       constructor(parameter){
    
        }
    
    }
    
    let j1 = new MyClass(parameter);
    let j2 = new MyClass(parameter);
    
    let a = 1; 
    
    let b = 2;
    
    function hi() {
        if (envt == true) {
            let something = j${ a }.parameter;
        } else {
            let something = j${ b }.parameter;
        }
        alert(something);
    }

Sorry I hope you understand, I am new in js, Thanks

nepko
  • 21
  • 4
  • [“Variable” variables in JavaScript](https://stackoverflow.com/q/5187530) you better use an array or an object to store your objects and retrieve them. That is if you absolutely *have to* look them up. Maybe you don't and you can use them directly. It's hard to tell because this is not a [mcve] - your code doesn't work even if we disregard the `${}` notatio. – VLAZ May 28 '21 at 04:57

2 Answers2

0

This is not possible. j is not a variable which is defined, however you are calling it and thus it would result in an undefined variable being called.

and with this "new variable" use ${} to call the correct object from my class

Why not send the object right away? Like this:

class MyClass(){
   constructor(parameter){

    }

}

let j1 = new MyClass(parameter);
let j2 = new MyClass(parameter);


function hi() {
    if (envt == true) {
        let something = j1.parameter;
    } else {
        let something = j2.parameter;
    }
    alert(something);
}

However let's say you want to do it the way you want it, you could do such thing:

let a = 1;
let b = 2;

function hi() {
    if (envt == true) {
        let something = [`j${a}`].parameter;
    } else {
        let something = [`j${b}`].parameter;
    }
    alert(something);
}
-1

just do:

[`j${variable}`].parameter
andriusain
  • 1,211
  • 10
  • 18
  • Thanks, but this ("[j${variable}]") return a String and I need return a Object, and this is my test : /// let d11111 = new dialogos(1,"SR GRAND ESTO A FUNCIONADO", 0); //let aux_txt = [`d${pj_seleccionado_id}${tipo_escena}${contador_escena}${contador_escena_img}${contador_dialogo}`] .contenido; alert(aux_txt); return: undefinded PS: from alerts i already know all the ${x} are 1 , but thanks, i thinking arrays maybe works – nepko May 28 '21 at 01:19
  • what you have is not what I posted, you are saying "[j${variable}]" whereas I said something different, take a closer look ;) – andriusain May 28 '21 at 01:38
  • let d_1_1_1 = new dialogos(1, "PRIMERA DIAGLO DE LA PRIMER ESCENA", 0); let d1 = 1; let d2 = 1; let d3 = 1; function accion_siguiente_escena() { let aux_txt = [`d_${d1}_${d2}_${d3}`].contenido; alert(aux_txt); let caja_dialogo = document.querySelector("#caja_dialogo"); caja_dialogo.innerHTML = aux_txt; } this is weird my text have the backtips but in the coment is not, but this is my last test and have the backtips in [`d_${d1}_${d2}_${d3}`].contenido; extrange =_( and yes, undefinded =_( this is not my day – nepko May 28 '21 at 02:07