Rename a variable using Parameters of a Function // Javascript [closed]

2

This is just an example:

function myfunction(u)

e+"u" = 1
x+"u" = 1
o+"u" = 1

//ai chamando ela...

myfunction(1)

e1 = 1
x1 = 1
o1 = 1 

Would you like to make it valid or do something similar?

    
asked by anonymous 16.03.2017 / 17:29

2 answers

1

Javascript has objects , based on keys and values such that I believe to be more useful for what you are trying to do . That way, you can add n keys, and then retrieve them in several ways. Based on your example:

var meuObjeto = {}

function myfunction(u) {
  meuObjeto['e'+ u] = 1;
  meuObjeto['x' + u] = 1;
  meuObjeto['o' + u] = 1;
}

myfunction(1);
console.log(meuObjeto);
    
16.03.2017 / 17:49
0

You can use the window variable as global scope and change the property of the object, it would look something like:

function myfunction(u){
  window['e'+u] = 1;
  window['x'+u] = 2;
  window['o'+u] = 3;
}

myfunction(1)
console.log(e1); //1
console.log(x1); //2
console.log(o1); //3
    
16.03.2017 / 19:01