onclick=“” vs event handler

前端 未结 8 2199
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:20

If I want a function to be executed, I prefer doing inline js:

Click me

becaus

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 00:42

    I see some saying there needs to be a separation of concerns regarding Presentation and Business Logic.

    OP in his example does indeed show this separation! There is no logic in the inline event handler, but merely a function reference/call that will get executed on the "click" event... the logic itself can be separately maintained elsewhere.

    I personally prefer this approach due to logical flow discoverability. If I have never seen an application before... the first place I'm going to begin my code traversal is in the DOM, and right there it will be clear which event handlers are in play and which functions are providing the logic to the handlers. With using, say a "JQuery.On" eventing approach, by just glancing through the html you would have no idea which handlers are actually wired up and providing functionality.

    Inline event handlers that simply provide pointers to functions is simply wiring up the events and not leaking logic into presentation.

提交回复
热议问题