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

86
Visualizações
An argument with default value precedes non-default argument in JavaScript

In JavaScript it's possible to define a function like this:

function func(a = 10, b)
{
  return a + b;
}

How does one call this function by only specifying the value for b?

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

0

It'd only be possible by explicitly passing undefined, and by also providing a default for the second argument.

function func(a = 10, b) {
  return a + b;
}

console.log(func(undefined, 5));

But this is quite weird - it's very confusing. I'd highly recommend not doing this at all - either put the arguments with default values at the end, or in an object.

function func({ a = 10, b }) {
  return a + b;
}

console.log(func({ b: 5 }));

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are okay with currying the first parameter:

bind saves the function with the arguments supplied, so when one calls it, it calls it with the arguments applied. The first argument is what to bind this to, so the second argument will bind to a:

function func(a, b) { 
    return a + b;
}

newFunc = func.bind(null, 10)
newFunc(2)
12
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