Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Cómo fallar si falta un parámetro de función

Cuando hago una función, verifico que los parámetros requeridos estén completos como se ve a continuación.

Pregunta

Realmente no es una buena manera, ya que todo lo que hace es asignar un valor predeterminado a un parámetro faltante. Prefiero que la ejecución se detenga y me diga a qué función le falta un parámetro. ¿Cómo se hace eso, si es posible?

 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 answers
Answer question

0

Lanzar un error si no encuentra el valor.

 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")

Puede crear una función para verificar esto.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!