DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> node.js中的events.emitter.removeListener方法教程使用說明詳解
node.js中的events.emitter.removeListener方法教程使用說明詳解
編輯:關於JavaScript     

方法說明:

移除指定事件的某個監聽器。

語法:

代碼如下:
emitter.removeListener(event, listener)

接收參數:

event            (string)             事件類型

listener         (function)        已注冊過的監聽器

例子:

代碼如下:
var callback = function(stream) {
  console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

源碼:

代碼如下:
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
  var list, position, length, i;
  if (!util.isFunction(listener))
    throw TypeError('listener must be a function');
  if (!this._events || !this._events[type])
    return this;
  list = this._events[type];
  length = list.length;
  position = -1;
  if (list === listener ||
      (util.isFunction(list.listener) && list.listener === listener)) {
    delete this._events[type];
    if (this._events.removeListener)
      this.emit('removeListener', type, listener);
  } else if (util.isObject(list)) {
    for (i = length; i-- > 0;) {
      if (list[i] === listener ||
          (list[i].listener && list[i].listener === listener)) {
        position = i;
        break;
      }
    }
    if (position < 0)
      return this;
    if (list.length === 1) {
      list.length = 0;
      delete this._events[type];
    } else {
      list.splice(position, 1);
    }
    if (this._events.removeListener)
      this.emit('removeListener', type, listener);
  }
  return this;
};

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved