最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年12月10日 09:54:31   投稿:junjie  
這篇文章主要介紹了node.js中的emitter.on方法使用說(shuō)明,本文介紹了emitter.on的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下

方法說(shuō)明:

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

語(yǔ)法:

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

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

接收參數(shù):

event            (string)             事件類型
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;
};

相關(guān)文章

最新評(píng)論

故城县| 中江县| 景宁| 合江县| 正阳县| 隆安县| 北京市| 万安县| 凉山| 西青区| 玉龙| 商南县| 大姚县| 林甸县| 武强县| 台东县| 永靖县| 古浪县| 乌鲁木齐县| 彝良县| SHOW| 元阳县| 北碚区| 招远市| 灵丘县| 绥江县| 禹城市| 沙河市| 忻城县| 江西省| 西丰县| 阳山县| 桓仁| 慈溪市| 西宁市| 新民市| 津市市| 蓝田县| 喀喇沁旗| 禄丰县| 牙克石市|