创建一个具有 emit、on 和 off 方法的事件总线。
emit
on
off
使用 Object.create() 并将参数设置为 null ,创建一个不从 Object.prototype 继承属性的空 hub 对象。
Object.create()
null
Object.prototype
hub
对于 emit,根据 event 参数解析处理程序数组,然后使用 Array.prototype.forEach() 传入数据作为参数运行每个处理程序。
event
Array.prototype.forEach()
对于 on,如果事件尚不存在,则为其创建一个数组,然后使用 Array.prototype.push() 将处理程序添加到该数组中。
Array.prototype.push()
对于 off,使用 Array.prototype.findIndex() 找到事件数组中处理程序的索引,并使用 Array.prototype.splice() 删除它。
Array.prototype.findIndex()
Array.prototype.splice()