概述
EventTarget
类是 Openlayers 中许多类实现的基类,主要和事件监听触发有关。
源码剖析
Disposable
类
EventTarget
类继承于Disposable
类,其实现如下:
class Disposable {constructor() {this.disposed = false;}dispose() {if (!this.disposed) {this.disposed = true;this.disposeInternal();}}disposeInternal() {}
}
Disposable
类中定义了一个disposed
变量,默认值为false
,两个方法dispose()
和disposeInternal()
。
dispose
译为处置,由此推定Disposable
类就是用于在函数调用后执行某些clean up
操作。
Target
类
EventTarget
类即target
实现如下:
class Target extends Disposable {constructor(target) {super();this.eventTarget_ = target;this.pendingRemovals_ = null;this.dispatching_ = null;this.listeners_ = null;}addEventListener(type, listener) {if (!type || !listener) {return;}const listeners = this.listeners_ || (this.listeners_ = {});const listenersForType = listeners[type] || (listeners[type] = []);if (!listenersForType.