Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

bug in step1-03,Extra brackets #96

Closed
yinsang opened this issue Mar 6, 2019 · 3 comments
Closed

bug in step1-03,Extra brackets #96

yinsang opened this issue Mar 6, 2019 · 3 comments

Comments

@yinsang
Copy link

yinsang commented Mar 6, 2019

https://github.com/Microsoft/frontend-bootcamp/blob/master/step1-03/demo/README.md

const button = document.querySelector('.submit');
button.onclick = displayMatches();

It's wrong to add brackets after displayMatches.

@fodra fodra mentioned this issue Mar 6, 2019
@ahtee
Copy link

ahtee commented Mar 6, 2019

Can you go into further detail on why? Syntactically it makes sense that displayMatches is a function when including parentheses() and variables or constants can be quickly seen as such because they wont have parentheses()

@ecraig12345
Copy link
Member

@yinsang Good catch, thanks!

@ahtee JavaScript has first-class functions, meaning that functions can be assigned to variables or passed as parameters the same as any other object or value. In this case, what we want to do is set the displayMatches function itself (not the return value from calling it) to be the button's onclick handler. The button will call displayMatches later, when it's clicked.

I don't think the step text should go into all that detail (unless it turns out to be a common point of confusion), but I updated the wording a bit to hopefully clarify what's going on.

In our example, we want to trigger a function when a button is clicked. To do this, we first need to get a reference to the button. We can use the querySelector method of the browser-provided document global variable to get that reference. Then we can set our displayMatches function to be the button's onclick handler.

@micahgodbolt
Copy link
Member

fixed in #98

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants