[ad_1]
A jewel from Chris Ferdinandi which details how to use custom events to hook into web components. More importantly, Chris carefully explains Why Custom events are more suitable than callback functions.
With a typical JavaScript library, you pass callbacks as part of the instantiation process. […] However, since web components instantiate themselves, there is no easy way to do this.
There is a way to use callback functions, but there is no “easy” way to do it.
JavaScript provides developers with the ability to emit custom events that developers can access using the
Element.addEventListener()
Procedure.We can use custom events to allow developers to intervene in the code we write and execute more code when something happens. They provide a really flexible way to extend the functionality of a library or code base.
Don’t miss the nugget about Canceling custom events!
Direct link →
[ad_2]
Source link