So to simplify your question you want to know what is the difference between using a function with and without parentheses.
Say we have a function logText() that looks something like this for example
function logText() {
console.log("Hello world!")
}
Then we run the in the console like this
logText()
We get this sent back
"Hello world!"
Now when we do this in the console
logText
We get back a reference to the function that looks like this
ƒ logText() {
console.log("Hello world!")
}
ƒ → function
What is the difference between this
cartLogic() {
clearCartBtn.addEventListener('click', this.clearCart);
}
And
cartLogic() {
clearCartBtn.addEventListener('click', () => {
this.clearCart()
});
}
The biggest difference between them both is the first example clearCart() is called immediately while the second example is wrapped in an anonymouse function and clearCart() is not being called immediately.
Does this change how things work?
Well not really, everything will still work the same way but why don't you have to add parathenese at the end of clearCart because this is refferencing the function so we can think of:
cartLogic() {
clearCartBtn.addEventListener('click', this.clearCart);
}
As
cartLogic() {
clearCartBtn.addEventListener('click', () => {
// All code for your function
...
});
}
But this makes a difference in how we code when the the function (in this case addEventListener()) sends some value back to the callback in this case will most likey be the event (e) being sent back to the call back.
Lets assume your clearCart() has a param called 'e' or 'event'
if we use your first example we do not have to decare e/event or parse the event ourselfs it will parsed for us but with your seconded example you would have to do something like this:
cartLogic() {
clearCartBtn.addEventListener('click', e => {
this.clearCart(e)
});
}
Why doesn't the second example not work without parentheses?
Because like I said early in the answer a function without parentheses is a reference to an function while a function with parentheses will be run.
We use functions without parentheses when we do not want to call the function instead we want to pass a reference of your function.
for example:
logText.length
This is parsing an reference of the function to the length method.
length used on a function will return the number of arguments expected by the function.
This is getting quite long so I'll end it here for now if there is something I didn't say or didn't say well leave a comment and I'll add it on :)
Also saw a comment on someone else's answer about the context of this and in my opinion that's not really related to this that's more about the difference between how you declare a function () => {} or function () {}.