I was wondering if someone could elaborate on what the event object actually is. I have looked at many sources that all seem to say it is an object that holds information about the current event.
My question is, what is it really? Is it a single global object that holds every event type? Or is there an event object for each unique event that occurs. I have seen in code you can write things like event.type, event.cancelBubble etc. does this mean I can access the event anytime and modify it from anywhere in the code? I have seen in examples of functions accepting an event as an argument, but why should this be if it is a global variable accessible anywhere?
I know this seems a bit broad but most sources seem to not go too much into how the event object works on a lower level. So if someone could clear this confusion for me that would be appreciated.
You are referring to the Window.event, which is no longer recommended for use. It is from a time when JavaScript had simpler goals and looser scope.
The read-only
Windowpropertyeventreturns theEventwhich is currently being handled by the site's code. Outside the context of an event handler, the value is alwaysundefined.
For new code, do not rely on the 'global' event being available and instead use the Event object passed to the event handler.
event is something that signifies something has happened.
If you click,type,resize, something loads etc. all of these are events. When you have a listener on any of these events, you are telling that you want something to happen in reaction to this event. The listener function has an argument event. This event will have details about the event that triggered it.
All these events implement from the same Event interface. So that is what you might be confusing it with. A bunch of event types - MouseEvent, ClipboardEvent etc. implement this. And events like click,dbclick,keyup etc. are then events using these interfaces. So it is like a chain you see. The properties you mentioned might belong to any of the interface at top level and are available at these specific events.
Note, you can create events too. This is helpful when you want to trigger events on your own. You can start with new Event().