@1k/emitter 中文文档教程
@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 tocb
- the function to call when event is emittedctx
- (OPTIONAL) - the context to bind the event cb to
once(event, cb[, ctx])
一个事件 仅订阅一个事件一次
event
- the name of the event to subscribe tocb
- the function to call when event is emittedctx
- (optional) - the context to bind the event cb to
off(event[, cb])
退订一个事件或所有事件。 如果未提供 cb,它会取消您对所有事件的订阅。
event
- the name of the event to unsubscribe fromcb
- the function used when binding to the event
emit(event[, arguments…])
触发命名事件
event
- the event name to emitarguments...
- any number of arguments to pass to the event subscribers
License
@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 tocb
- the function to call when event is emittedctx
- (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 tocb
- the function to call when event is emittedctx
- (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 fromcb
- the function used when binding to the event
emit(event[, arguments…])
Trigger a named event
event
- the event name to emitarguments...
- any number of arguments to pass to the event subscribers