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

157
Views
¿Podemos simplificar este código? show = typeof show == 'indefinido' ? indefinido : !mostrar
function myToggle(show) { //...bunch of codes here... show = typeof show == 'undefined' ? undefined : !show $(el).toggleClass('d-none', show ) }

En la función anterior, show se invertirá (booleano) solo si está definido, luego pasará a toggleClass() de jQuery. d-none es una clase de Bootstrap para ocultar el elemento.

De modo que cuando:

 myToggle() //this will toggle class d-none to the element myToggle(1) //this will remove class d-none from the element myToggle(0) //this will give class d-none to the element

Entonces mi pregunta, ¿podemos simplificar este código?

 show = typeof show == 'undefined' ? undefined : !show
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Comencemos con el hecho de que esto debería ser === , no solo == . Casi nunca quieres == en JS (no es Java o C++, siempre usa === a menos que sepas por qué no deberías ). Con eso, no necesita un typeof de control: solo verifique si su var no está undefined , directamente.

 show = show === undefined? show : !show;

Sin embargo, esto ignora cómo funciona .toggleClass() de jQuery . No toma "verdadero o falso o indefinido" como segundo argumento, toma un booleano, y "mostrar" es bastante explícito en su intención, por lo que la simplificación real es la más obvia:

 show = !!show;
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!