window click event vs a lot of buttons click event

66 views Asked by At

I'm making an online store, and there will be a lot of "add to cart" buttons. When one gets clicked, a function will fire.

Which is more efficient, to make one window click event, then check if the button got clicked, and fire the function. Or should each button have it's click event?

1

There are 1 answers

1
etov On

From an Object-Oriented perspective, it would seem more reasonable to hook the button click event, because the button as an entity should encapsulate the logic relevant to it.

Then, for instance, if there are multiple types of buttons with slightly different characteristics, there will be a clear separation of logic, instead of it all remaining concentrated in a single monolithic piece of code.