js-events-emitter

1.0.0 • Public • Published

简介

javascript 订阅事件和发布事件类

  • 相对于 EventEmitter 这个库可以传 this,改变回调 this 指向

起步

npm i js-events-emitter -S

使用

const JsEventsEmitter= require('js-events-emitter')
// 或
import JsEventsEmitter from 'js-events-emitter'

const events = new JsEventsEmitter()

let _this = new Object()

events.on('custom-event', _this, callbackFunc, ['附加参数1', '附加参数2'])

events.emit('custom-event', ['附加参数3', '附加参数4'])

function callbackFunc(param1, param2, param3, param4) {
  console.log(param1, param2, param3, param4) //'附加参数1', '附加参数2','附加参数3', '附加参数4'
  console.log(this) // this === _this
}

JsEventsEmitter

  • 详情: 订阅事件和发布事件

方法

on

  • 详情: 订阅事件
参数 类型 默认值 描述
type string 事件名
scope * 作用域
callback function 回调
[params] array [] 回调参数

once

  • 详情: 订阅事件-回调只执行一次
参数 类型 默认值 描述
type string 事件名
scope * 作用域
callback function 回调
[params] array [] 回调参数

emit

  • 详情: 发布事件
参数 类型 默认值 描述
type string
[params] array [] 附加到回调的参数

getListener

  • 详情: 获取指定事件的监听器数组列表
参数 类型 默认值 描述
type string 事件名
  • 返回:array|undefined- 返回列表

getListenerList

  • 详情: 获取全部事件的全部监听者的列表

  • 返回:object


getEventNames

  • 详情: 获取已经监听的事件名列表

  • 返回:array- 事件名数组


addListener

  • 详情: 添加到事件等待处理触发列表
参数 类型 默认值 描述
type string
[listenerParams] object { callback: null, scope: this, params: null, isOnce: false } 触发事件的参数

offAll

  • 详情: 清除所有订阅
参数 类型 默认值 描述
type string,null 事件名, 如果指定事件则移除该事件的所有监听器,如果不传则移除所有事件的所有监听器,并注销事件

off

  • 详情: 清除对应的订阅
参数 类型 默认值 描述
type string 事件名
removeCallback function 监听器的回调函数

Package Sidebar

Install

npm i js-events-emitter

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

87.1 kB

Total Files

15

Last publish

Collaborators

  • dolami