• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

299
Views
Refactorización de JavaScript, declaración de variable abreviada

Tengo el siguiente código que funciona como se esperaba:

 (async () => { const first = document.getElementById("first"), second = document.getElementById("second"); try { first.innerHTML = await (await fetch('https://myAPI.com/1')).text(); second.innerHTML = await (await fetch('https://myAPI.com/2')).text(); } catch { first.innerHTML = 'Error'; second.innerHTML = 'Error'; } })();

Pero tengo curiosidad por saber si hay una forma de acortar la declaración de mis variables, algo como esto (que no funciona):

 (async () => { const [first, second] = document.getElementById("[first, second]"); try { first.innerHTML = await (await fetch('https://myAPI.com/1')).text(); second.innerHTML = await (await fetch('https://myAPI.com/2')).text(); } catch { first.innerHTML = 'Error'; second.innerHTML = 'Error'; } })();

Solo tengo curiosidad por saber si puedo deshacerme de esa segunda aparición de 'document.getElementById' para hacer que la línea sea más compacta.

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La forma más concisa de hacerlo es usando lo siguiente:

 const [first, second] = document.querySelectorAll("#first, #second");
about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error