Namespace: Basic:isEventTarget

Basic:isEventTarget

Source:

Methods

isEventTarget(value) → {Boolean}

Returns if a value is an EventTarget, which means that it is able to dispatch and receive events. This is determined via duck-typing and not via class inheritance check, since this method is not about type-safety, but the question if we can use the target for events, which is simply determined by three essential object methods: addEventListener, removeEventListener and dispatchEvent. All objects supporting these are fine with us.

Parameters:
Name Type Description
value *

the value to check

Source:
Returns:
true if value supports event methods
Type
Boolean
Example
if( isEventTarget(target) ){
  target.dispatchEvent(new CustomEvent('foobar'));
}