What would dynamic javascript parameters be?

Asked

Viewed 287 times

3

What would be dynamic parameters in javascript?

  • 2

    it is worth reading, it is not exactly what you asked "but as it were" http://answall.com/questions/93144/como-o-jquery-faz-os-par%C3%A2meters-being-dynamic

  • 1

    Explain a little better what you refer to by dynamic parameters.

  • 1
  • William, your question is with 4 votes to be put on hold (it can be reopened), but at the same time has 3 positive votes and a very cool answer; it will probably be open and will be popular. But for the benefit of others who have the same doubt as you and find the solution here, can you describe better why you asked? It’s just that when the title and description are exactly the same it gets really weird...

1 answer

3


Dynamic parameters occur when your function accepts a variable number of arguments, type:

minhaFuncao(x)
minhaFuncao(x, y, z)

The number of arguments can be finite, type 1 or 2 or 3 arguments accepted, but can also be indeterminate, open. See two examples:

function recado(mensagem, nome){
    if(typeof nome == 'undefined'){
        nome = 'Amigo';
    }
    alert('Recado para ' + nome + ': ' + mensagem);
}
recado('Tenha um bom dia.'); //resultado: 'Recado para Amigo: Tenha um bom dia.'
recado('OK', 'Guilherme'); //resultado: 'Recado para Guilherme: OK'

function soma(inicio){
    var resposta = inicio;
    if(arguments.length > 1) {
        for(var i=1; i<arguments.length; i++){
            resposta += arguments[i];
        }
    }
    return resposta;
}
soma(3); // retorna 3
soma(100, 10, 2, 3); // retorna 115

Browser other questions tagged

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