Make Variable with multiple names and a single Value String

1

It would be possible to create a single variable that has the mutual influence of other names on it.

In other words, different names for the same statement var , and in the end will have the same value.

Example

I'm doing this:

var perfume = "Racco";

var colonia = "Racco";

var roll-on = "Racco";

I want something like this:

var perfume, colonia, roll-on = "Racco"
  

It does not matter! Any of the three names corresponds to the same new String(); .

     

Just a var , with multiple names, to call only a single Value .

How can I do this? After declaring more than one variable on the same line bring a single value to all of them.

    
asked by anonymous 07.06.2016 / 05:09

1 answer

5

You can assign the same value to multiple variables at one time as follows:

var perfume = colonia = rollon = "Racco"

But in the above case, if the variables colonia and rollon have not yet declared, they will be global variables. If you want to create variables for the local scope and assign the same value to them you need to do both in separate steps:

var perfume, colonia, rollon;
perfume = colonia = rollon = "Racco";

If in fact what you want is to change a single variable and have the value "automatically" changed in the other ones: In javascript this is not possible with primitive types, only with complex types, as they are passed as reference:

var objeto = {};
objeto.x = 10;
var objeto_2 = objeto;
objeto_2.x = 20;
console.log(objeto.x); // objeto.x == 20
    
07.06.2016 / 05:58