国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

node.js中的emitter.on方法使用說(shuō)明

2024-05-06 16:11:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了node.js中的emitter.on方法使用說(shuō)明,本文介紹了emitter.on的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
 
 

方法說(shuō)明:

為指定事件注冊(cè)一個(gè)監(jiān)聽(tīng)器。

語(yǔ)法:

 

復(fù)制代碼代碼如下:

emitter.on(event, listener)
emitter.addListener(event, listener)

 

接收參數(shù):

event            (string)             事件類(lèi)型
listener         (function)         觸發(fā)事件時(shí)的回調(diào)函數(shù)

例子:

 

復(fù)制代碼代碼如下:

server.on('connection', function (stream) {
  console.log('someone connected!');
});

 

源碼:

 

復(fù)制代碼代碼如下:

EventEmitter.prototype.addListener = function(type, listener) {
  var m;
  if (!util.isFunction(listener))
    throw TypeError('listener must be a function');
  if (!this._events)
    this._events = {};
  // To avoid recursion in the case that type === "newListener"! Before
  // adding it to the listeners, first emit "newListener".
  if (this._events.newListener)
    this.emit('newListener', type,
              util.isFunction(listener.listener) ?
              listener.listener : listener);
  if (!this._events[type])
    // Optimize the case of one listener. Don't need the extra array object.
    this._events[type] = listener;
  else if (util.isObject(this._events[type]))
    // If we've already got an array, just append.
    this._events[type].push(listener);
  else
    // Adding the second element, need to change to array.
    this._events[type] = [this._events[type], listener];
  // Check for listener leak
  if (util.isObject(this._events[type]) && !this._events[type].warned) {
    var m;
    if (!util.isUndefined(this._maxListeners)) {
      m = this._maxListeners;
    } else {
      m = EventEmitter.defaultMaxListeners;
    }
    if (m && m > 0 && this._events[type].length > m) {
      this._events[type].warned = true;
      console.error('(node) warning: possible EventEmitter memory ' +
                    'leak detected. %d listeners added. ' +
                    'Use emitter.setMaxListeners() to increase limit.',
                    this._events[type].length);
      console.trace();
    }
  }
  return this;
};

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 潜江市| 曲靖市| 宁晋县| 长兴县| 吉安市| 琼中| 资阳市| 嘉祥县| 达拉特旗| 东丽区| 陆川县| 九台市| 禄劝| 扬中市| 江油市| 当阳市| 开封县| 潜山县| 衡山县| 淮滨县| 秭归县| 镇赉县| 花莲县| 陕西省| 公安县| 拉孜县| 克东县| 武胜县| 惠安县| 清原| 阜宁县| 奇台县| 阜宁县| 胶州市| 册亨县| 囊谦县| 进贤县| 井陉县| 老河口市| 洪洞县| 封开县|