Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jorge Velasco
Jorge Velasco
13,773 Points

When Guil passed the method "generateHTML", how come he didn't need to pass data as the parameter?

I mostly understand the event that's taking place bellow, but how come in generateHTML he didn't need explicitly pass the data to generate the HTML?

btn.addEventListener('click', (event) => { getJSON(astrosUrl, (json /'json here represents the data returned from the getJSON method'/) => { json.people.map(astronaut => { getJSON(wikiUrl + astronaut.name, generateHTML); }); }) event.target.remove(); // removes the button from the page after completing the event });

This is how the generateHTML method is written for reference:

function generateHTML(data /'data is an optional parameter'/) { const section = document.createElement('section'); peopleList.appendChild(section); // Check if request returns a 'standard' page from Wiki if (data.type === 'standard') { section.innerHTML = <img src=${data.thumbnail.source}> <h2>${data.title}</h2> <p>${data.description}</p> <p>${data.extract}</p> ; } else { section.innerHTML = <img src="img/profile.jpg" alt="ocean clouds seen from space"> <h2>${data.title}</h2> <p>Results unavailable for ${data.title}</p> ${data.extract_html} ; } }

1 Answer

Steven Parker
Steven Parker
231,271 Points

When the method is invoked by getJSON, the data is passed to it at that time. You don't see that here because only the reference to the function is being passed for now and it is not actually called until later.