Because in this context, addItem
is used as a function reference rather than the return value of the function.
If you did this:
addButton.addEventListener('click', addItem());
Then addItem
would be executed straight away, and whenever addButton
was clicked, the return value of addItem
(which is undefined
) would be called. This would result in an error, because undefined
is not a function.
Here, you're saying when I click addButton
, lookup the function reference I passed, and execute it.
You can also write this two different ways:
addButton.addEventListener('click', "addItem()");
addButton.addEventListener('click', function() {
addItem();
});
Both of the above will still result in the same output as your original code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…