Custom events in client side javaScript
In client side javaScript there is the custom event constructor that can be used to create my own events that can be attached to html elements. I then in my own code define the conditions that will be used to trigger these kinds of custom events by calling the dispatch event method of the element that I attached a handler for the custom event.
There are a number of other ways of creating custom events when it comes to using a framework like phaser and threejs, often such frameworks have a system for creating user defined events for various kinds of things that happen in such frameworks, as well as additional user space code that will run on top of them. There are also ways of doing this in a nodejs environment when it comes to the events module which would be the node built in way of how to go about making user define events in a sever side javaScript environment. However there is also the idea of making a core JavaScript solution that will work in the browser as well as node also.
In this post I will be mainly focusing on the ways to go about making custom events in just plain old vanilla client side javaScript in a web browser. This will also include some examples where I am making my own system in core javaScript which can then be used in any javaScript environment. The basic process is similar to what needs to happen when simulating a built in event such as a click event on a div element for example. Make sure there is an event handler attached to the element in question for the event, create an event object for the event, and then call the dispatch event method of the element and pass the event object to that method.