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

Asked

Viewed 67 times

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, can you do that? 'Cause the code below didn’t 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);

3 answers

6


Variables can only be changed in the way you want them to be if they can be referenced, that is if they are part of a uncivilized type.

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

In fact even passing a not primitive like {} or [] for example, if you do a = 'foo' delete the previous reference.

What can you do:

What is possible is to alter properties of a non-reprimitive (object, array, function) within the function and this causes 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);

2

What is happening is that the function setVariableValue() receives only the value of the variables test, test2 and test3. Then within the function, its operation is executed successfully, but on top of variables that will cease to exist as soon as the function returns (will leave the scope).

Therefore, normally we call a function, already assigning the result to the variable itself:

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

In this case, your role 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.

1

Inside 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 answers, are invasive because you will need to change the structure. I preferred to keep as close as possible to what you need. This is known as "variables" or "variable".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.