I have a function which I intend to fire on several different Events.
Importantly, the function may also be invoked directly.
But I need the function to know which event it is being fired by... or whether it is being invoked directly.
I realised I could (somehow) harness the Event Object and use something like:
const myFunction = (e) => {
if ((e !== undefined) && (e.hasOwnProperty('type')) {
switch (e.type) {
case ('click'): console.log('Click Event'); break;
case ('mouseover'): console.log('Mouseover Event'); break;
}
}
else {
console.log('Separately invoked');
}
}
This isn't terrible, but it's more verbose than I want it to be and I'm not too keen on trying to verify the Event Object by checking if e is both not undefined and has a particular property... this all seems a very roundabout approach to verifying the presence of an Event Object.
Is there a better way to check for the [presence and type] or [absence] of an Event Object?