Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

JASON LEE
JASON LEE
11,766 Points

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
212,140 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.