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

Why isn't the addEventListener using a callback function? @ 5:35

https://teamtreehouse.com/library/create-a-reusable-fetch-function#questions @ 5:35

When Guil writes the addEventListener function, why doesn't he use a callback? I thought that's how AddEventListener works.

for instance the code he has is ...

select.addEventListener('change', fetchBreedImage)

But shouldn't it be ...

select.addEventListener('change', () => {fetchBreedImage()} )

Both seem to produce the same result in this scenario, but I know in certain scenarios it produces different outputs. Not sure when and why...

1 Answer

Steven Parker
Steven Parker
229,744 Points

Just by itself, "fetchBreedImage" is a callback, since it is a reference to a function (not an invocation).

When you write "() => {fetchBreedImage()} ", you are creating a new, anonymous function which will call fetchBreedImage when it runs. This has the same effect but with an extra step involved.