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

206
Visualizações
How to fail if missing a function parameter

When make a function, I check that the required parameters are filled in as seen below.

Question

It is not really a good way, as all it does is assigning a default value to a missing parameter. I'd much rather have that execution stops and tells me which function is missing a parameter. How is that done, if possible?

func({
  system: "test1",
  type: "test2",
  summary: "test3",
  description: "test4",
});

function func (c) {
  c = c || {};
  c.system = c.system || "Missing system";
  c.type = c.type || 'Missing type';
  c.summary = c.summary || 'Missing summary';
  c.description = c.description || 'Missing description';

  console.log(c);
  console.log(c.system);
  console.log(c.type);
  console.log(c.summary);
  console.log(c.description);
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Throwing an error if doesnt find the value.

if(!c.system) throw new Error("Missing system"); // This would fail if c.system is falsy

// In this case can be used:

if(!c.hasOwnProperty("system")) throw new Error("Missing system")

Can create a function to check this.

function Check(objt, key, messageError){
    if(!objt.hasOwnProperty(key)) throw new Error(messageError)
}
Check(c, "system", "Missing system");
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