How to define the value of a variable from a function?

5

I want something like, a function that will be passed a variable as a parameter and a value that will be applied to it, how can I do that? For the code below did not work:

function setVariableValue(variable, value) {
    variable = value;
}

var test; var test2; var test3;

setVariableValue(test, 20);
setVariableValue(test2, "Ola");
setVariableValue(test3, 1000);

document.writeln(test);
document.writeln(test2);
document.writeln(test3);
    
asked by anonymous 18.06.2017 / 03:08

3 answers

6

Variables can only be changed the way you want them if they can be referenced, ie if they are part of a non-primitive type .

When you use function(a, b){ and you pass a primitive type to the function, it only follows the value with the variable and no connection to that variable outside the function.

Actually even passing a non-primitive as {} or [] for example, if you do a = 'foo' delete the previous reference.

What you can do:

What is possible is to change properties of a non-primitive ( object , array , function ) within the function and cause changes outside it.

function sobreEscrever(variable, value) {
  variable = value;
}

function mudarPropriedade(variable, propriedade, value) {
  variable[propriedade] = value;
}

var primitivo = 'foo'
var objeto = {};

sobreEscrever(primitivo, 20);
console.log(primitivo);

sobreEscrever(objeto, "Ola");
console.log(objeto);

var objeto = {};
mudarPropriedade(objeto, 'chave', 1000);
console.log(objeto);
    
18.06.2017 / 08:14
2

What is happening is that the setVariableValue() function only receives the value of the variables test , test2 and test3 . So within the function, its operation is executed successfully, but over variables that will cease to exist once the function returns (they will be out of scope).

  

So, we usually call a function, already assigning the result to the variable itself:

test = setVariableValue(test, 20); // atribui o resultado a 'test'

In this case, your function should have a return :

function setVariableValue(variable, value) {
    // return necessário para que a função retorne algum valor!
    return variable * value;
}

var test = 10;
test = setVariableValue(test, 20);
document.writeln(test); // Resultado: 200
  

A second way to solve - Use the variable itself within the function:

var test;
function setVariableValue(value) {
    test = value;
}

setVariableValue(12);
document.writeln(test); // Resultado: 12
  

And a third way - Using objects:

function setVariableValue(variable, value) {
    variable.value = value;
}

var test = [], test2 = [], test3 = [];

setVariableValue(test, 20);
setVariableValue(test2, "Ola");
setVariableValue(test3, 1000);

document.writeln(test.value);
document.writeln(test2.value);
document.writeln(test3.value);

As objects, what is passed to the function ends up being a reference, and when changing an attribute of this object, the change persists.

    
18.06.2017 / 08:03
1

Within the function, use:

window[variable] = value;

Example

function setVariableValue(variable, value) {
    window[variable] = value;
}

To set the value

test = 30;
console.log(test); // imprime 30

setVariableValue("test", 20); //deve estar delimitado por aspas.

console.log(test); // imprime 20

The other forms presented in other responses are invasive because you need to modify the structure. I preferred to keep it as close as I could. This is known as "variable variables" or "variable variable."

    
18.06.2017 / 07:44