Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
How to bound variable which I pass through function to a specific parameter

Very stupid question. For example I have a variable data and I have function DoSomething with two parameters par1 and par2. they are both optional parameters that would be null if I pass nothing. But I have only one variable data which I want to bound to the second parameter without touching first parameter. In different language I should write something like DoSomething(par2 = data) or DoSomething(par2: data) but all these examples will raise exception Uncaught SyntaxError: missing ). So what I did wrong?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If you have two optional parameters in JavaScript, just pass undefined to the first argument.

DoSomething(undefined, data);

undefined is the default value of arguments when they aren't passed in (DoSomething() would have both arguments be undefined) except when you set default values for optional arguments.

Another alternative is to pass in the arguments as an object, which requires changing the definition of the function and all calls to it: (?? is the Nullish coalescing operator

function DoSomething(args) {
  const par1 = args.par1 ?? "default value";
  const par2 = args.par2 ?? "default value";
}

// ....
DoSomething({ par2: "custom value" });
DoSomething({ par1: "custom value", par2: "customValue" });

Using TypeScript or JSDocs allows you to give guidance on how to send arguments in the IDE

about 4 years ago · Juan Pablo Isaza Relatório

0

You always can pass undefined or null as first parameter. SO it will be something like this:

DoSomething(undefined, data);
about 4 years ago · Juan Pablo Isaza Relatório

0

If you have multiple optional parameters, if you don't want to pass undefined.. you should use an object as the parameter.

// instead of DoSomething(param1, param2)
function DoSomething(param = {}) {
  const { param1, param2 } = param;

  //..do something with param1 and param2
}

DoSomething({ param2: 'somevalue' })

If you consider using typescript, it's even better because you can add the type to the param.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda