Problems with js event binding

there are multiple button, events in a page that each button responds to. There are two scenarios:

  1. bind different methods to each button
  2. bind the same method and use parameters to distinguish which button triggered
  3. .

which scheme is better

Mar.04,2021

use a public method to pass parameters differently, so that the code is easy to maintain.


normally binds each button to its own method, unless the corresponding events for each button are very similar, and the second way can be used if the method can be reused.


the description is not careful enough, let's break it down.

  • if the two buttons have the same function or are similar to , bind to the same method.
  • if the two buttons have different functions , then bind to different methods.

for example, the edit and remove buttons should be bound to different methods because their functions are different.


according to your description, I understand that each button function is different, I think the first one is better, clearer, different functions are separated, otherwise you have to maintain a super long method.

Menu