How do I save the parameters of a function in a variable?

3

I want to execute this function coord(343,5,6,2,86528,875433, longitude, latitude, 'COORDENADAS');

But I wanted to save the parameters to run the function afterwards. How to save?

I tried var x = 343,5,6,2,86528,875433, longitude, latitude, 'COORDENADAS' but it gives error.

    
asked by anonymous 27.07.2015 / 15:55

2 answers

3

You can access the object arguments of the function:

var args; // Variável no escopo global para salvar os parâmetros
function MinhaFuncao(x, y, z, w, k, j){
    args = arguments; // Salva os parâmetros passado
    console.log(arguments);
    document.getElementById('log').innerHTML = arguments[1];
}
//; Executa função primeira vez
MinhaFuncao(1, 'arroz', 3.14, 'macarrão', true, 'joão', 'picolé', 'maracujá');

// Alterando o terceiro argumento
args[2] = 'Feijão';

// Pode chama-la novamente usando o apply:
if (args != undefined)
   MinhaFuncao.apply(this, args); 
   // Primeiro parâmentro é o contexto, 
   // o segundo é o array de argumentos
<p id="log"></p>

Return of object arguments :

Arguments[8] {
  0: 1
  1: "arroz"
  2: 3.14
  3: "macarrão"
  4: true
  5: "joão"
  6: "picolé"
  7: "maracujá"
}
    
27.07.2015 / 16:42
2

It would look like this:

var p1 = 343, p2 = 5, p3 = 6, p4 = 2, p5 = 86528, p6 = 875433, 
    p7 = longitude, p8 = latitude, p9 = 'COORDENADAS';

Note that you have 9 parameters there, you need to save in 9 variables. I could use the array too, but I think it's even better in this case that there are 9 variables. The only thing that should change in relation to what I did is give more meaningful names to the variables, indicating what each parameter is.

You may think it is not a good solution, but I do not see a better one, even though there are other creatives. I do not really know why you need this, you may not even have this need. It does not make much sense to do this. It's possible that all the design of the application is wrong.

    
27.07.2015 / 16:00