@1k/emitter 中文文档教程

发布于 5年前 浏览 33 项目主页 更新于 3年前

@1k/emitter

一个微小的(小于 1k)事件发射器。

Install

npm

npm install @1k/emitter --save

Usage

var Emitter = require('@1k/emitter');
var emitter = new Emitter();

emitter.on('some-event', function (arg1, arg2, arg3) {
 //
});

emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');

Instance Methods

on(event, cb[, ctx])

订阅

  • event - the name of the event to subscribe to
  • cb - the function to call when event is emitted
  • ctx - (OPTIONAL) - the context to bind the event cb to

once(event, cb[, ctx])

一个事件 仅订阅一个事件一次

  • event - the name of the event to subscribe to
  • cb - the function to call when event is emitted
  • ctx - (optional) - the context to bind the event cb to

off(event[, cb])

退订一个事件或所有事件。 如果未提供 cb,它会取消您对所有事件的订阅。

  • event - the name of the event to unsubscribe from
  • cb - the function used when binding to the event

emit(event[, arguments…])

触发命名事件

  • event - the event name to emit
  • arguments... - any number of arguments to pass to the event subscribers

License

MIT

@1k/emitter

A tiny (less than 1k) event emitter.

Install

npm

npm install @1k/emitter --save

Usage

var Emitter = require('@1k/emitter');
var emitter = new Emitter();

emitter.on('some-event', function (arg1, arg2, arg3) {
 //
});

emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');

Instance Methods

on(event, cb[, ctx])

Subscribe to an event

  • event - the name of the event to subscribe to
  • cb - the function to call when event is emitted
  • ctx - (OPTIONAL) - the context to bind the event cb to

once(event, cb[, ctx])

Subscribe to an event only once

  • event - the name of the event to subscribe to
  • cb - the function to call when event is emitted
  • ctx - (optional) - the context to bind the event cb to

off(event[, cb])

Unsubscribe from an event or all events. If no cb is provided, it unsubscribes you from all events.

  • event - the name of the event to unsubscribe from
  • cb - the function used when binding to the event

emit(event[, arguments…])

Trigger a named event

  • event - the event name to emit
  • arguments... - any number of arguments to pass to the event subscribers

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文