这个事件管理器是three.js所使用,其作者本身就是three.js的作者mrdoob,它没有使用DOM和ie的接口,而是自己写了一套方法,简单但是很实用
/**
* @author mrdoob / http://mrdoob.com/
*https://github.com/mrdoob/eventdispatcher.js github地址
*/
function EventDispatcher() {}
Object.assign( EventDispatcher.prototype, {//向EventDispatcher添加以下方法
addEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) this._listeners = {};
var listeners = this._listeners;
if ( listeners[ type ] === undefined ) {
listeners[ type ] = [];
}
if ( listeners[ type ].indexOf( listener ) === - 1 ) {
listeners[ type ].push( listener );
}
},
hasEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) return false;
var listeners = this._listeners;
return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1;
},
removeEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) return;
var listeners = this._listeners;
var listenerArray = listeners[ type ];
if ( listenerArray !== undefined ) {
var index = listenerArray.indexOf( listener );
if ( index !== - 1 ) {//若该类事件的数组中存在,则删除该监听器
listenerArray.splice( index, 1 );
}
}
},
dispatchEvent: function ( event ) {//调用某类事件
if ( this._listeners === undefined ) return;
var listeners = this._listeners;
var listenerArray = listeners[ event.type ];
if ( listenerArray !== undefined ) {
event.target = this;
var array = listenerArray.slice( 0 );//创建复制一个新数组
for ( var i = 0, l = array.length; i < l; i ++ ) {
array[ i ].call( this, event );
}
}
}
} );