{"id":31465,"date":"2024-11-25T15:19:36","date_gmt":"2024-11-25T07:19:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31465\/"},"modified":"2024-11-25T15:19:36","modified_gmt":"2024-11-25T07:19:36","slug":"js%e7%9a%84eventemitter%e4%bd%bf%e7%94%a8%e6%ad%a5%e5%a5%8f%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31465\/","title":{"rendered":"JS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765JS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3\uff0c\u4f7f\u7528EventEmitter\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">2\u4e2a\u591a\u6708\u524d\u628a Github \u4e0a\u7684 eventemitter3 \u548c  \u4e0b\u7684\u4e8b\u4ef6\u6a21\u5757 events \u7684\u6e90\u7801\u6284\u4e86\u4e00\u904d\uff0c\u624d\u7ec8\u4e8e\u5bf9  \u4e8b\u4ef6\u6709\u6240\u4e86\u89e3\u3002<\/p>\n<p style=\"text-align: left\">\u4e0a\u4e2a\u5468\u672b\u82b1\u70b9\u65f6\u95f4\u6839\u636e\u4e4b\u524d\u770b\u6e90\u7801\u7684\u7406\u89e3\u81ea\u5df1\u7528 ES6 \u5b9e\u73b0\u4e86\u4e00\u4e2a eventemitter8\uff0c\u7136\u540e\u4e5f\u53d1\u5e03\u5230 npm \u4e0a\u4e86\uff0c\u8ba9\u6211\u6bd4\u8f83\u610f\u5916\u7684\u662f\u624d\u53d1\u5e03\u4e24\u5929\u5728\u6ca1\u6709 readme \u4ecb\u7ecd\uff0c\u6ca1\u6709\u4efb\u4f55\u5ba3\u4f20\u7684\u60c5\u51b5\u4e0b\u5c45\u7136\u670945\u4e2a\u4e0b\u8f7d\uff0c\u6211\u5f88\u597d\u5947\u90fd\u662f\u8c01\u4e0b\u8f7d\u7684\uff0c\u4f1a\u4e0d\u4f1a\u7528\u3002\u6211\u82b1\u4e86\u4e0d\u5c11\u65f6\u95f4\u534a\u6284\u534a\u539f\u521b\u7684\u4e00\u4e2a JavaScript \u65f6\u95f4\u5904\u7406\u5e93 now.js (npm \u4f20\u9001\u95e8\uff1anow.js) \uff0c\u5728\u6211\u5927\u529b\u5ba3\u4f20\u7684\u60c5\u51b5\u4e0b\uff0c4\u4e2a\u6708\u7684\u4e0b\u8f7d\u91cf\u624d177\u3002\u771f\u662f\u6709\u5fc3\u683d\u82b1\u82b1\u4e0d\u5f00\uff0c\u65e0\u5fc3\u63d2\u67f3\u67f3\u6210\u836b\uff01<\/p>\n<p style=\"text-align: left\">eventemitter8 \u5927\u90e8\u5206\u662f\u6211\u6839\u636e\u770b\u6e90\u7801\u7406\u89e3\u540e\u5199\u51fa\u6765\u7684\uff0c\u6709\u4e00\u4e9b\u65b9\u6cd5\u5982listeners\uff0clistenerCount \u548c eventNames \u4e00\u4e0b\u5b50\u60f3\u4e0d\u8d77\u6765\u5230\u5e95\u505a\u4ec0\u4e48\uff0c\u56de\u5934\u91cd\u67e5\u3002\u6d4b\u8bd5\u7528\u4f8b\u4e0d\u5c11\u662f\u53c2\u8003\u4e86 eventemitter3\uff0c\u5728\u6b64\u5bf9 eventemitter3 \u7684\u5f00\u53d1\u8005\u4eec\u548c Node.js \u4e8b\u4ef6\u6a21\u5757\u7684\u5f00\u53d1\u8005\u4eec\u8868\u793a\u611f\u8c22\uff01<\/p>\n<p style=\"text-align: left\"><strong>\u4e0b\u9762\u6765\u8bb2\u8bb2\u6211\u5bf9 JavaScript \u4e8b\u4ef6\u7684\u7406\u89e3\uff1a<\/strong><\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/2b7bbbdd2dcde98d0f7bfc948a936a4a-0.png\" class=\"aligncenter\" title=\"JS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3\u63d2\u56fe\" alt=\"JS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p style=\"text-align: left\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0cJavaScript \u4e8b\u4ef6\u6700\u6838\u5fc3\u7684\u5305\u62ec\u4e8b\u4ef6\u76d1\u542c (addListener)\u3001\u4e8b\u4ef6\u89e6\u53d1 (emit)\u3001\u4e8b\u4ef6\u5220\u9664 (removeListener)\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u4e8b\u4ef6\u76d1\u542c(addListener)<\/strong><\/p>\n<p style=\"text-align: left\">\u9996\u5148\uff0c\u76d1\u542c\u80af\u5b9a\u8981\u6709\u76d1\u542c\u7684\u76ee\u6807\uff0c\u6216\u8005\u8bf4\u662f\u5bf9\u8c61\uff0c\u90a3\u4e3a\u4e86\u8fbe\u5230\u533a\u5206\u76ee\u6807\u7684\u76ee\u7684\uff0c\u540d\u5b57\u662f\u4e0d\u53ef\u5c11\u7684\uff0c\u6211\u4eec\u5b9a\u4e49\u4e3a type\u3002<\/p>\n<p style=\"text-align: left\">\u5176\u6b21\uff0c\u76d1\u542c\u7684\u76ee\u6807\u4e00\u5b9a\u8981\u6709\u67d0\u79cd\u52a8\u4f5c\uff0c\u5bf9\u5e94\u5230 JavaScript \u91cc\u5b9e\u9645\u4e0a\u5c31\u662f\u67d0\u79cd\u65b9\u6cd5\uff0c\u8fd9\u91cc\u5b9a\u4e49\u4e3a fn\u3002<\/p>\n<p style=\"text-align: left\">\u8b6c\u5982\u53ef\u4ee5\u76d1\u542c\u4e00\u4e2a type \u4e3a add\uff0c\u65b9\u6cd5\u4e3a\u67d0\u4e00\u4e2a\u53d8\u91cf a \u503c\u52a01\u7684\u65b9\u6cd5 fn = () =&gt; a + 1\u7684\u4e8b\u4ef6\u3002\u5982\u679c\u6211\u4eec\u8fd8\u60f3\u76d1\u542c\u4e00\u4e2a\u4f7f\u53d8\u91cf b \u52a02\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u7b2c\u4e00\u53cd\u5e94\u53ef\u80fd\u662f\u521b\u5efa\u4e00\u4e2a type \u4e3a add2\uff0c\u65b9\u6cd5 \u4e3a fn1 = () =&gt; b + 2 \u7684\u4e8b\u4ef6\u3002\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u8fd9\u592a\u6d6a\u8d39\u4e86\uff0c\u6211\u80fd\u4e0d\u80fd\u53ea\u76d1\u542c\u4e00\u4e2a\u540d\u5b57\uff0c\u8ba9\u5b83\u6267\u884c\u591a\u4e8e\u4e00\u4e2a\u65b9\u6cd5\u7684\u4e8b\u4ef6\u3002\u5f53\u7136\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u90a3\u4e48\u600e\u4e48\u505a\u5462\uff1f<\/strong><\/p>\n<p style=\"text-align: left\">\u5f88\u7b80\u5355\uff0c\u628a\u76d1\u542c\u7684\u65b9\u6cd5\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u91cc\uff0c\u987a\u5e8f\u6267\u884c\u5c31\u53ef\u4ee5\u4e86\u3002\u4ee5\u4e0a\u4f8b\u5b50\u53d8\u4e3a type \u4e3a add\uff0c\u65b9\u6cd5\u4e3a[fn, fn1]\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c\u8981\u7ec6\u5206\u7684\u8bdd\u8fd8\u53ef\u4ee5\u5206\u4e3a\u53ef\u4ee5\u65e0\u9650\u6b21\u6267\u884c\u7684\u4e8b\u4ef6 on \u548c \u53ea\u5141\u8bb8\u6267\u884c\u4e00\u6b21\u7684\u4e8b\u4ef6 once (\u6267\u884c\u5b8c\u540e\u7acb\u5373\u5c06\u4e8b\u4ef6\u5220\u9664)\u3002\u5f85\u540e\u8be6\u8ff0\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u4e8b\u4ef6\u89e6\u53d1(emit)<\/strong><\/p>\n<p style=\"text-align: left\">\u5355\u6709\u4e8b\u4ef6\u76d1\u542c\u662f\u4e0d\u591f\u7684\uff0c\u5fc5\u987b\u8981\u6709\u4e8b\u4ef6\u89e6\u53d1\u624d\u80fd\u7b97\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002emit \u5c31\u662f\u53bb\u89e6\u53d1\u76d1\u542c\u7684\u7279\u5b9a type \u5bf9\u5e94\u7684\u5355\u4e2a\u4e8b\u4ef6\u6216\u8005\u4e00\u7cfb\u5217\u4e8b\u4ef6\u3002\u62ff\u524d\u9762\u7684\u4f8b\u5b50\u6765\u8bf4\u5355\u4e2a\u4e8b\u4ef6\u5c31\u662f\u53bb\u6267\u884c fn\uff0c\u4e00\u7cfb\u5217\u4e8b\u4ef6\u5c31\u662f\u53bb\u904d\u5386\u6267\u884c fn \u548c fn1\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u4e8b\u4ef6\u5220\u9664(removeListener)<\/strong><\/p>\n<p style=\"text-align: left\">\u4e25\u683c\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u4e8b\u4ef6\u76d1\u542c\u548c\u4e8b\u4ef6\u89e6\u53d1\u5df2\u7ecf\u80fd\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002\u4e8b\u4ef6\u5220\u9664\u53ef\u6709\u53ef\u65e0\u3002\u4f46\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u8fd8\u662f\u9700\u8981\u4e8b\u4ef6\u5220\u9664\u7684\u3002\u6bd4\u5982\u524d\u9762\u8bb2\u7684\u53ea\u5141\u8bb8\u6267\u884c\u4e00\u6b21\u4e8b\u4ef6 once\uff0c\u5982\u679c\u4e0d\u63d0\u4f9b\u5220\u9664\u65b9\u6cd5\uff0c\u5f88\u96be\u4fdd\u8bc1\u4f60\u4ec0\u4e48\u65f6\u5019\u4f1a\u518d\u6b21\u6267\u884c\u5b83\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u8981\u662f\u4e0d\u518d\u9700\u8981\u7684\u4e8b\u4ef6\uff0c\u6211\u4eec\u90fd\u5e94\u8be5\u53bb\u5220\u9664\u5b83\u3002<\/p>\n<p style=\"text-align: left\">\u6838\u5fc3\u90e8\u5206\u8bb2\u5b8c\uff0c\u4e0b\u9762\u7b80\u5355\u7684\u5bf9 eventemitter8\u7684\u6e90\u7801\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u6e90\u7801\u89e3\u6790<\/strong><\/p>\n<p style=\"text-align: left\">\u5168\u90e8\u6e90\u7801:<\/p>\n<pre>const toString = Object.prototype.toString;\r\nconst isType = obj =&gt; toString.call(obj).slice(8, -1).toLowerCase();\r\nconst isArray = obj =&gt; Array.isArray(obj) || isType(obj) === 'array';\r\nconst isNullOrUndefined = obj =&gt; obj === null || obj === undefined;\r\nconst _addListener = function(type, fn, context, once) {\r\n if (typeof fn !== 'function') {\r\n  throw new TypeError('fn must be a function');\r\n }\r\n fn.context = context;\r\n fn.once = !!once;\r\n const event = this._events[type];\r\n \/\/ only one, let `this._events[type]` to be a function\r\n if (isNullOrUndefined(event)) {\r\n  this._events[type] = fn;\r\n } else if (typeof event === 'function') {\r\n  \/\/ already has one function, `this._events[type]` must be a function before\r\n  this._events[type] = [event, fn];\r\n } else if (isArray(event)) {\r\n  \/\/ already has more than one function, just push\r\n  this._events[type].push(fn);\r\n }\r\n return this;\r\n};\r\nclass EventEmitter {\r\n constructor() {\r\n  if (this._events === undefined) {\r\n   this._events = Object.create(null);\r\n  }\r\n }\r\n addListener(type, fn, context) {\r\n  return _addListener.call(this, type, fn, context);\r\n }\r\n on(type, fn, context) {\r\n  return this.addListener(type, fn, context);\r\n }\r\n once(type, fn, context) {\r\n  return _addListener.call(this, type, fn, context, true);\r\n }\r\n emit(type, ...rest) {\r\n  if (isNullOrUndefined(type)) {\r\n   throw new Error('emit must receive at lease one argument');\r\n  }\r\n  const events = this._events[type];\r\n  if (isNullOrUndefined(events)) return false;\r\n  if (typeof events === 'function') {\r\n   events.call(events.context || null, rest);\r\n   if (events.once) {\r\n    this.removeListener(type, events);\r\n   }\r\n  } else if (isArray(events)) {\r\n   events.map(e =&gt; {\r\n    e.call(e.context || null, rest);\r\n    if (e.once) {\r\n     this.removeListener(type, e);\r\n    }\r\n   });\r\n  }\r\n  return true;\r\n }\r\n removeListener(type, fn) {\r\n  if (isNullOrUndefined(this._events)) return this;\r\n  \/\/ if type is undefined or null, nothing to do, just return this\r\n  if (isNullOrUndefined(type)) return this;\r\n  if (typeof fn !== 'function') {\r\n   throw new Error('fn must be a function');\r\n  }\r\n  const events = this._events[type];\r\n  if (typeof events === 'function') {\r\n   events === fn &amp;&amp; delete this._events[type];\r\n  } else {\r\n   const findIndex = events.findIndex(e =&gt; e === fn);\r\n   if (findIndex === -1) return this;\r\n   \/\/ match the first one, shift faster than splice\r\n   if (findIndex === 0) {\r\n    events.shift();\r\n   } else {\r\n    events.splice(findIndex, 1);\r\n   }\r\n   \/\/ just left one listener, change Array to Function\r\n   if (events.length === 1) {\r\n    this._events[type] = events[0];\r\n   }\r\n  }\r\n  return this;\r\n }\r\n removeAllListeners(type) {\r\n  if (isNullOrUndefined(this._events)) return this;\r\n  \/\/ if not provide type, remove all\r\n  if (isNullOrUndefined(type)) this._events = Object.create(null);\r\n  const events = this._events[type];\r\n  if (!isNullOrUndefined(events)) {\r\n   \/\/ check if `type` is the last one\r\n   if (Object.keys(this._events).length === 1) {\r\n    this._events = Object.create(null);\r\n   } else {\r\n    delete this._events[type];\r\n   }\r\n  }\r\n  return this;\r\n }\r\n listeners(type) {\r\n  if (isNullOrUndefined(this._events)) return [];\r\n  const events = this._events[type];\r\n  \/\/ use `map` because we need to return a new array\r\n  return isNullOrUndefined(events) ? [] : (typeof events === 'function' ? [events] : events.map(o =&gt; o));\r\n }\r\n listenerCount(type) {\r\n  if (isNullOrUndefined(this._events)) return 0;\r\n  const events = this._events[type];\r\n  return isNullOrUndefined(events) ? 0 : (typeof events === 'function' ? 1 : events.length);\r\n }\r\n eventNames() {\r\n  if (isNullOrUndefined(this._events)) return [];\r\n  return Object.keys(this._events);\r\n }\r\n}\r\nexport default EventEmitter;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4ee3\u7801\u5f88\u5c11\uff0c\u53ea\u6709151\u884c\uff0c\u56e0\u4e3a\u5199\u7684\u7b80\u5355\u7248\uff0c\u4e14\u7528\u7684 ES6\uff0c\u6240\u4ee5\u624d\u8fd9\u4e48\u5c11\uff1bNode.js\u7684\u4e8b\u4ef6\u548c eventemitter3\u53ef\u6bd4\u8fd9\u591a\u4e14\u590d\u6742\u4e0d\u5c11\uff0c\u6709\u5174\u8da3\u53ef\u81ea\u884c\u6df1\u5165\u7814\u7a76\u3002<\/p>\n<pre>const toString = Object.prototype.toString;\r\nconst isType = obj =&gt; toString.call(obj).slice(8, -1).toLowerCase();\r\nconst isArray = obj =&gt; Array.isArray(obj) || isType(obj) === 'array';\r\nconst isNullOrUndefined = obj =&gt; obj === null || obj === undefined;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8fd94\u884c\u5c31\u662f\u4e00\u4e9b\u5de5\u5177\u51fd\u6570\uff0c\u5224\u65ad\u6240\u5c5e\u7c7b\u578b\u3001\u5224\u65ad\u662f\u5426\u662f null \u6216\u8005 undefined\u3002<\/p>\n<pre>constructor() {\r\n if (isNullOrUndefined(this._events)) {\r\n  this._events = Object.create(null);\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u521b\u5efa\u4e86\u4e00\u4e2a EventEmitter \u7c7b\uff0c\u7136\u540e\u5728\u91cc\u521d\u59cb\u5316\u4e00\u4e2a\u7c7b\u7684 _events \u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u4e0d\u9700\u8981\u8981\u7ee7\u627f\u4efb\u4f55\u4e1c\u897f\uff0c\u6240\u4ee5\u7528\u4e86 Object.create(null)\u3002\u5f53\u7136\u8fd9\u91cc isNullOrUndefined(this._events) \u8fd8\u53bb\u5224\u65ad\u4e86\u4e00\u4e0b this._events \u662f\u5426\u4e3a undefined \u6216\u8005 null\uff0c\u5982\u679c\u662f\u624d\u9700\u8981\u521b\u5efa\u3002\u4f46\u8fd9\u4e0d\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u5b9e\u4f8b\u5316\u4e00\u4e2a EventEmitter \u90fd\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u7686\u4e3a\u521d\u59cb\u72b6\u6001\uff0cthis._events \u5e94\u8be5\u662f\u4e0d\u53ef\u80fd\u5df2\u7ecf\u5b9a\u4e49\u4e86\u7684\uff0c\u53ef\u53bb\u6389\u3002<\/p>\n<pre>addListener(type, fn, context) {\r\n return _addListener.call(this, type, fn, context);\r\n}\r\non(type, fn, context) {\r\n return this.addListener(type, fn, context);\r\n}\r\nonce(type, fn, context) {\r\n return _addListener.call(this, type, fn, context, true);\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u63a5\u4e0b\u6765\u662f\u4e09\u4e2a\u65b9\u6cd5 addListener\u3001on\u3001once \uff0c\u5176\u4e2d on \u662f addListener \u7684\u522b\u540d\uff0c\u53ef\u6267\u884c\u591a\u6b21\u3002once \u53ea\u80fd\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p style=\"text-align: left\">\u4e09\u4e2a\u65b9\u6cd5\u90fd\u7528\u5230\u4e86 _addListener \u65b9\u6cd5\uff1a<\/p>\n<pre>const _addListener = function(type, fn, context, once) {\r\n if (typeof fn !== 'function') {\r\n  throw new TypeError('fn must be a function');\r\n }\r\n fn.context = context;\r\n fn.once = !!once;\r\n const event = this._events[type];\r\n \/\/ only one, let `this._events[type]` to be a function\r\n if (isNullOrUndefined(event)) {\r\n  this._events[type] = fn;\r\n } else if (typeof event === 'function') {\r\n  \/\/ already has one function, `this._events[type]` must be a function before\r\n  this._events[type] = [event, fn];\r\n } else if (isArray(event)) {\r\n  \/\/ already has more than one function, just push\r\n  this._events[type].push(fn);\r\n }\r\n return this;\r\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u65b9\u6cd5\u6709\u56db\u4e2a\u53c2\u6570\uff0ctype \u662f\u76d1\u542c\u4e8b\u4ef6\u7684\u540d\u79f0\uff0cfn \u662f\u76d1\u542c\u4e8b\u4ef6\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0ccontext \u4fd7\u79f0\u7238\u7238\uff0c\u6539\u53d8 this \u6307\u5411\u7684\uff0c\u4e5f\u5c31\u662f\u6267\u884c\u7684\u4e3b\u4f53\u3002once \u662f\u4e00\u4e2a\u5e03\u5c14\u578b\uff0c\u7528\u6765\u6807\u5fd7\u662f\u5426\u53ea\u6267\u884c\u4e00\u6b21\u3002<br \/>\u9996\u5148\u5224\u65ad fn \u7684\u7c7b\u578b\uff0c\u5982\u679c\u4e0d\u662f\u65b9\u6cd5\uff0c\u629b\u51fa\u4e00\u4e2a\u7c7b\u578b\u9519\u8bef\u3002fn.context = context;fn.once = !!once \u628a\u6267\u884c\u4e3b\u4f53\u548c\u662f\u5426\u6267\u884c\u4e00\u6b21\u4f5c\u4e3a\u65b9\u6cd5\u7684\u5c5e\u6027\u3002const event = this._events[type] \u628a\u8be5\u5bf9\u5e94 type \u7684\u6240\u6709\u5df2\u7ecf\u76d1\u542c\u7684\u65b9\u6cd5\u5b58\u5230\u53d8\u91cf event\u3002<\/p>\n<pre>\/\/ only one, let `this._events[type]` to be a function\r\nif (isNullOrUndefined(event)) {\r\n this._events[type] = fn;\r\n} else if (typeof event === 'function') {\r\n \/\/ already has one function, `this._events[type]` must be a function before\r\n this._events[type] = [event, fn];\r\n} else if (isArray(event)) {\r\n \/\/ already has more than one function, just push\r\n this._events[type].push(fn);\r\n}\r\nreturn this;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5982\u679c type \u672c\u8eab\u6ca1\u6709\u6b63\u5728\u76d1\u542c\u4efb\u4f55\u65b9\u6cd5\uff0cthis._events[type] = fn \u76f4\u63a5\u628a\u76d1\u542c\u7684\u65b9\u6cd5 fn \u8d4b\u7ed9 type \u5c5e\u6027 \uff1b\u5982\u679c\u6b63\u5728\u76d1\u542c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5219\u628a\u8981\u6dfb\u52a0\u7684 fn \u548c\u4e4b\u524d\u7684\u65b9\u6cd5\u53d8\u6210\u4e00\u4e2a\u542b\u67092\u4e2a\u5143\u7d20\u7684\u6570\u7ec4 [event, fn]\uff0c\u7136\u540e\u518d\u8d4b\u7ed9 type \u5c5e\u6027\uff0c\u5982\u679c\u6b63\u5728\u76d1\u542c\u8d85\u8fc72\u4e2a\u65b9\u6cd5\uff0c\u76f4\u63a5 push \u5373\u53ef\u3002\u6700\u540e\u8fd4\u56de this \uff0c\u4e5f\u5c31\u662f EventEmitter \u5b9e\u4f8b\u672c\u8eab\u3002<\/p>\n<p style=\"text-align: left\">\u7b80\u5355\u6765\u8bb2\u4e0d\u7ba1\u662f\u76d1\u542c\u591a\u5c11\u65b9\u6cd5\uff0c\u90fd\u653e\u5230\u6570\u7ec4\u91cc\u662f\u6ca1\u5fc5\u8981\u50cf\u4e0a\u9762\u7ec6\u5206\u3002\u4f46\u6027\u80fd\u8f83\u5dee\uff0c\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\u65f6 key: fn \u7684\u6548\u7387\u6bd4 key: [fn] \u8981\u9ad8\u3002<\/p>\n<p style=\"text-align: left\">\u518d\u56de\u5934\u770b\u770b\u4e09\u4e2a\u65b9\u6cd5:<\/p>\n<pre>addListener(type, fn, context) {\r\n return _addListener.call(this, type, fn, context);\r\n}\r\non(type, fn, context) {\r\n return this.addListener(type, fn, context);\r\n}\r\nonce(type, fn, context) {\r\n return _addListener.call(this, type, fn, context, true);\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">addListener \u9700\u8981\u7528 call \u6765\u6539\u53d8 this \u6307\u5411\uff0c\u6307\u5230\u4e86\u7c7b\u7684\u5b9e\u4f8b\u3002once \u5219\u591a\u4f20\u4e86\u4e00\u4e2a\u6807\u5fd7\u4f4d true \u6765\u6807\u5fd7\u5b83\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21\u3002\u8fd9\u91cc\u4f60\u4f1a\u770b\u5230\u6211\u5728 addListener \u5e76\u6ca1\u6709\u4f20 false \u4f5c\u4e3a\u6807\u5fd7\u4f4d\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u6211\u61d2\uff0c\u4f46\u5e76\u4e0d\u4f1a\u5f71\u54cd\u5230\u7a0b\u5e8f\u7684\u903b\u8f91\u3002\u56e0\u4e3a\u524d\u9762\u7684 fn.once = !!once \u5df2\u7ecf\u80fd\u5f88\u597d\u7684\u5904\u7406\u4e0d\u4f20\u503c\u7684\u60c5\u51b5\u3002\u6ca1\u4f20\u503c !!once \u4e3a false\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u63a5\u4e0b\u6765\u8bb2 emit<\/strong><\/p>\n<pre>emit(type, ...rest) {\r\n if (isNullOrUndefined(type)) {\r\n  throw new Error('emit must receive at lease one argument');\r\n }\r\n const events = this._events[type];\r\n if (isNullOrUndefined(events)) return false;\r\n if (typeof events === 'function') {\r\n  events.call(events.context || null, rest);\r\n  if (events.once) {\r\n   this.removeListener(type, events);\r\n  }\r\n } else if (isArray(events)) {\r\n  events.map(e =&gt; {\r\n   e.call(e.context || null, rest);\r\n   if (e.once) {\r\n    this.removeListener(type, e);\r\n   }\r\n  });\r\n }\r\n return true;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4e8b\u4ef6\u89e6\u53d1\u9700\u8981\u6307\u5b9a\u5177\u4f53\u7684 type \u5426\u5219\u76f4\u63a5\u629b\u51fa\u9519\u8bef\u3002\u8fd9\u4e2a\u5f88\u5bb9\u6613\u7406\u89e3\uff0c\u4f60\u90fd\u6ca1\u6709\u6307\u5b9a\u540d\u79f0\uff0c\u6211\u600e\u4e48\u77e5\u9053\u8be5\u53bb\u6267\u884c\u8c01\u7684\u4e8b\u4ef6\u3002if (isNullOrUndefined(events)) return false\uff0c\u5982\u679c type \u5bf9\u5e94\u7684\u65b9\u6cd5\u662f undefined \u6216\u8005 null \uff0c\u76f4\u63a5\u8fd4\u56de false \u3002\u56e0\u4e3a\u538b\u6839\u6ca1\u6709\u5bf9\u5e94 type \u7684\u65b9\u6cd5\u53ef\u4ee5\u6267\u884c\u3002\u800c emit \u9700\u8981\u77e5\u9053\u662f\u5426\u88ab\u6210\u529f\u89e6\u53d1\u3002<\/p>\n<p style=\"text-align: left\">\u63a5\u7740\u5224\u65ad evnts \u662f\u4e0d\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5982\u679c\u662f\uff0c events.call(events.context || null, rest) \u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5982\u679c\u6307\u5b9a\u4e86\u6267\u884c\u4e3b\u4f53\uff0c\u7528 call \u6539\u53d8 this \u7684\u6307\u5411\u6307\u5411 events.context \u4e3b\u4f53\uff0c\u5426\u5219\u6307\u5411 null \uff0c\u5168\u5c40\u73af\u5883\u3002\u5bf9\u4e8e\u6d4f\u89c8\u5668\u73af\u5883\u6765\u8bf4\u5c31\u662f window\u3002\u5dee\u70b9\u5fd8\u4e86 rest \uff0crest \u662f\u65b9\u6cd5\u6267\u884c\u65f6\u7684\u5176\u4ed6\u53c2\u6570\u53d8\u91cf\uff0c\u53ef\u4ee5\u4e0d\u4f20\uff0c\u4e5f\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u3002\u6267\u884c\u7ed3\u675f\u540e\u5224\u65ad events.once \uff0c\u5982\u679c\u4e3a true \uff0c\u5c31\u7528 removeListener \u79fb\u9664\u8be5\u76d1\u542c\u4e8b\u4ef6\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c evnts \u662f\u6570\u7ec4\uff0c\u903b\u8f91\u4e00\u6837\uff0c\u53ea\u662f\u9700\u8981\u904d\u5386\u6570\u7ec4\u53bb\u6267\u884c\u6240\u6709\u7684\u76d1\u542c\u65b9\u6cd5\u3002<\/p>\n<p style=\"text-align: left\">\u6210\u529f\u6267\u884c\u7ed3\u675f\u540e\u8fd4\u56de true \u3002<\/p>\n<pre>removeListener(type, fn) {\r\n if (isNullOrUndefined(this._events)) return this;\r\n \/\/ if type is undefined or null, nothing to do, just return this\r\n if (isNullOrUndefined(type)) return this;\r\n if (typeof fn !== 'function') {\r\n  throw new Error('fn must be a function');\r\n }\r\n const events = this._events[type];\r\n if (typeof events === 'function') {\r\n  events === fn &amp;&amp; delete this._events[type];\r\n } else {\r\n  const findIndex = events.findIndex(e =&gt; e === fn);\r\n  if (findIndex === -1) return this;\r\n  \/\/ match the first one, shift faster than splice\r\n  if (findIndex === 0) {\r\n   events.shift();\r\n  } else {\r\n   events.splice(findIndex, 1);\r\n  }\r\n  \/\/ just left one listener, change Array to Function\r\n  if (events.length === 1) {\r\n   this._events[type] = events[0];\r\n  }\r\n }\r\n return this;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">removeListener \u63a5\u6536\u4e00\u4e2a\u4e8b\u4ef6\u540d\u79f0 type \u548c\u4e00\u4e2a\u5c06\u8981\u88ab\u79fb\u9664\u7684\u65b9\u6cd5 fn \u3002if (isNullOrUndefined(this._events)) return this \u8fd9\u91cc\u8868\u793a\u5982\u679c EventEmitter \u5b9e\u4f8b\u672c\u8eab\u7684 _events \u4e3a null \u6216\u8005 undefined \u7684\u8bdd\uff0c\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u76d1\u542c\uff0c\u76f4\u63a5\u8fd4\u56de this \u3002<\/p>\n<p style=\"text-align: left\">if (isNullOrUndefined(type)) return this \u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u4e8b\u4ef6\u540d\u79f0\uff0c\u4e5f\u76f4\u63a5\u8fd4\u56de this \u3002<\/p>\n<pre>if (typeof fn !== 'function') {\r\n throw new Error('fn must be a function');\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">fn \u5982\u679c\u4e0d\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u76f4\u63a5\u629b\u51fa\u9519\u8bef\uff0c\u5f88\u597d\u7406\u89e3\u3002<\/p>\n<p style=\"text-align: left\">\u63a5\u7740\u5224\u65ad type \u5bf9\u5e94\u7684 events \u662f\u4e0d\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u662f\uff0c\u5e76\u4e14 events === fn \u8bf4\u660e type \u5bf9\u5e94\u7684\u65b9\u6cd5\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\uff0c\u7b49\u4e8e\u6211\u4eec\u6307\u5b9a\u8981\u5220\u9664\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65f6\u5019 delete this._events[type] \u76f4\u63a5\u5220\u9664\u6389 this._events \u5bf9\u8c61\u91cc type \u5373\u53ef\u3002<\/p>\n<p style=\"text-align: left\">\u6240\u6709\u7684 type \u5bf9\u5e94\u7684\u65b9\u6cd5\u90fd\u88ab\u79fb\u9664\u540e\u3002\u60f3\u4e00\u60f3 this._events[type] = undefined \u548c delete this._events[type] \u4f1a\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/p>\n<p style=\"text-align: left\">\u5dee\u5f02\u662f\u5f88\u5927\u7684\uff0cthis._events[type] = undefined \u4ec5\u4ec5\u662f\u5c06 this._events \u5bf9\u8c61\u91cc\u7684 type \u5c5e\u6027\u8d4b\u503c\u4e3a undefined \uff0ctype \u8fd9\u4e00\u5c5e\u6027\u4f9d\u7136\u5360\u7528\u5185\u5b58\u7a7a\u95f4\uff0c\u4f46\u5176\u5b9e\u5df2\u7ecf\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002\u5982\u679c\u8fd9\u6837\u7684 type \u4e00\u591a\uff0c\u6709\u53ef\u80fd\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002delete this._events[type] \u5219\u76f4\u63a5\u5220\u9664\uff0c\u4e0d\u5360\u5185\u5b58\u7a7a\u95f4\u3002\u524d\u8005\u4e5f\u662f Node.js \u4e8b\u4ef6\u6a21\u5757\u548c eventemitter3 \u65e9\u671f\u5b9e\u73b0\u7684\u505a\u6cd5\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c events \u662f\u6570\u7ec4\uff0c\u8fd9\u91cc\u6211\u6ca1\u6709\u7528 isArray \u8fdb\u884c\u5224\u65ad\uff0c\u800c\u662f\u76f4\u63a5\u7528\u4e00\u4e2a else \uff0c\u539f\u56e0\u662f this._events[type] \u7684\u8f93\u5165\u9650\u5236\u5728 on \u6216\u8005 once \u4e2d\uff0c\u800c\u5b83\u4eec\u5df2\u7ecf\u9650\u5236\u4e86 this._events[type] \u53ea\u80fd\u662f\u65b9\u6cd5\u7ec4\u6210\u7684\u6570\u7ec4\u6216\u8005\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6700\u591a\u52a0\u4e0a\u4e0d\u5c0f\u5fc3\u6216\u8005\u4eba\u4e3a\u8d4b\u6210 undefined \u6216 null \u7684\u60c5\u51b5\uff0c\u4f46\u8fd9\u4e2a\u60c5\u51b5\u6211\u4eec\u4e5f\u5728\u524d\u9762\u5224\u65ad\u8fc7\u4e86\u3002<\/p>\n<p style=\"text-align: left\">\u56e0\u4e3a isArray \u8fd9\u4e2a\u5de5\u5177\u65b9\u6cd5\u5176\u5b9e\u8fd0\u884c\u6548\u7387\u662f\u4e0d\u9ad8\u7684\uff0c\u4e3a\u4e86\u8ffd\u6c42\u4e00\u4e9b\u6548\u7387\uff0c\u5728\u4e0d\u5f71\u54cd\u8fd0\u884c\u903b\u8f91\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e0d\u7528 isArray \u3002\u800c\u4e14 typeof events === &#8216;function&#8217; \u7528 typeof \u5224\u65ad\u65b9\u6cd5\u4e5f\u6bd4 isArray \u7684\u6548\u7387\u8981\u9ad8\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4e0d\u5148\u5224\u65ad\u662f\u5426\u662f\u6570\u7ec4\u7684\u539f\u56e0\u3002\u7528 typeof \u53bb\u5224\u65ad\u4e00\u4e2a\u65b9\u6cd5\u4e5f\u6bd4 Object.prototype.toSting.call(events) === &#8216;[object Function] \u6548\u7387\u8981\u9ad8\u3002\u4f46\u6570\u7ec4\u4e0d\u80fd\u7528 typeof \u8fdb\u884c\u5224\u65ad\uff0c\u56e0\u4e3a\u8fd4\u56de\u7684\u662f object\uff0c \u8fd9\u4f17\u6240\u5468\u77e5\u3002\u867d\u7136\u5982\u6b64\uff0c\u5728\u6211\u9762\u8bd5\u8fc7\u7684\u5f88\u591a\u4eba\u4e2d\uff0c\u4ecd\u7136\u6709\u5f88\u591a\u4eba\u4e0d\u77e5\u9053\u3002\u3002\u3002<\/p>\n<p style=\"text-align: left\">const findIndex = events.findIndex(e =&gt; e === fn) \u6b64\u5904\u7528 ES6 \u7684\u6570\u7ec4\u65b9\u6cd5 findIndex \u76f4\u63a5\u53bb\u67e5\u627e fn \u5728 events \u4e2d\u7684\u7d22\u5f15\u3002\u5982\u679c findIndex === -1 \u8bf4\u660e\u6211\u4eec\u6ca1\u6709\u627e\u5230\u8981\u5220\u9664\u7684 fn \uff0c\u76f4\u63a5\u8fd4\u56de this \u5c31\u597d\u3002\u5982\u679c findIndex === 0 \uff0c\u662f\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0cshift \u5254\u9664\uff0c\u5426\u5219\u7528 splice \u5254\u9664\u3002\u56e0\u4e3a shift \u6bd4 splice \u6548\u7387\u9ad8\u3002<\/p>\n<p style=\"text-align: left\">findIndex \u7684\u6548\u7387\u5176\u5b9e\u6ca1\u6709 for \u5faa\u73af\u53bb\u67e5\u627e\u7684\u9ad8\uff0c\u6240\u4ee5 eventemitter8 \u7684\u6548\u7387\u5728\u6211\u6ca1\u6709\u505a benchmark \u4e4b\u524d\u6211\u5c31\u77e5\u9053\u80af\u5b9a\u4f1a\u6bd4 eventemitter3 \u6548\u7387\u8981\u4f4e\u4e0d\u5c11\u3002\u4e0d\u90a3\u4e48\u8ffd\u6c42\u6267\u884c\u6548\u7387\u65f6\u5f53\u7136\u662f\u7528\u6700\u61d2\u7684\u65b9\u5f0f\u6765\u5199\u6700\u723d\u3002\u6240\u8c13\u7684\u61d2\u5373\u6b63\u4e49\u3002\u3002\u3002<\/p>\n<p style=\"text-align: left\">\u6700\u540e\u8fd8\u5f97\u5224\u65ad\u79fb\u9664 fn \u540e events \u5269\u4f59\u7684\u6570\u91cf\uff0c\u5982\u679c\u53ea\u6709\u4e00\u4e2a\uff0c\u57fa\u4e8e\u4e4b\u524d\u8981\u505a\u7684\u4f18\u5316\uff0cthis._events[type] = events[0] \u628a\u542b\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u53d8\u6210\u4e00\u4e2a\u65b9\u6cd5\uff0c\u964d\u7ef4\u6253\u51fb\u4e00\u4e0b\u3002\u3002\u3002<\/p>\n<p style=\"text-align: left\">\u6700\u540e\u7684\u6700\u540e return this \u8fd4\u56de\u81ea\u8eab\uff0c\u94fe\u5f0f\u8c03\u7528\u8fd8\u80fd\u7528\u5f97\u4e0a\u3002<\/p>\n<pre>removeAllListeners(type) {\r\n if (isNullOrUndefined(this._events)) return this;\r\n \/\/ if not provide type, remove all\r\n if (isNullOrUndefined(type)) this._events = Object.create(null);\r\n const events = this._events[type];\r\n if (!isNullOrUndefined(events)) {\r\n  \/\/ check if type is the last one\r\n  if (Object.keys(this._events).length === 1) {\r\n   this._events = Object.create(null);\r\n  } else {\r\n   delete this._events[type];\r\n  }\r\n }\r\n return this;\r\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">removeAllListeners \u6307\u7684\u662f\u8981\u5220\u9664\u4e00\u4e2a type \u5bf9\u5e94\u7684\u6240\u6709\u65b9\u6cd5\u3002\u53c2\u6570 type \u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u672a\u6307\u5b9a type \uff0c\u9ed8\u8ba4\u628a\u6240\u6709\u7684\u76d1\u542c\u4e8b\u4ef6\u5220\u9664\uff0c\u76f4\u63a5 this._events = Object.create(null) \u64cd\u4f5c\u5373\u53ef\uff0c\u8ddf\u521d\u59cb\u5316 EventEmitter \u7c7b\u4e00\u6837\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c events \u65e2\u4e0d\u662f null \u4e14\u4e0d\u662f undefined \u8bf4\u660e\u6709\u53ef\u5220\u9664\u7684 type \uff0c\u5148\u7528 Object.keys(this._events).length === 1 \u5224\u65ad\u662f\u4e0d\u662f\u6700\u540e\u4e00\u4e2a type \u4e86\uff0c\u5982\u679c\u662f\uff0c\u76f4\u63a5\u521d\u59cb\u5316 this._events = Object.create(null)\uff0c\u5426\u5219 delete this._events[type] \u76f4\u63a5\u5220\u9664 type \u5c5e\u6027\uff0c\u4e00\u6b65\u5230\u4f4d\u3002<\/p>\n<p style=\"text-align: left\">\u6700\u540e\u8fd4\u56de this \u3002<\/p>\n<p style=\"text-align: left\">\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6240\u6709\u7684\u6838\u5fc3\u529f\u80fd\u5df2\u7ecf\u8bb2\u5b8c\u3002<\/p>\n<pre>listeners(type) {\r\n if (isNullOrUndefined(this._events)) return [];\r\n const events = this._events[type];\r\n \/\/ use `map` because we need to return a new array\r\n return isNullOrUndefined(events) ? [] : (typeof events === 'function' ? [events] : events.map(o =&gt; o));\r\n}\r\nlistenerCount(type) {\r\n if (isNullOrUndefined(this._events)) return 0;\r\n const events = this._events[type];\r\n return isNullOrUndefined(events) ? 0 : (typeof events === 'function' ? 1 : events.length);\r\n}\r\neventNames() {\r\n if (isNullOrUndefined(this._events)) return [];\r\n return Object.keys(this._events);\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">listeners \u8fd4\u56de\u7684\u662f type \u5bf9\u5e94\u7684\u6240\u6709\u65b9\u6cd5\u3002\u7ed3\u679c\u90fd\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u8fd4\u56de\u7a7a\u6570\u7ec4\uff1b\u5982\u679c\u53ea\u6709\u4e00\u4e2a\uff0c\u628a\u5b83\u7684\u65b9\u6cd5\u653e\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u8fd4\u56de\uff1b\u5982\u679c\u672c\u6765\u5c31\u662f\u4e00\u4e2a\u6570\u7ec4\uff0cmap \u8fd4\u56de\u3002\u4e4b\u6240\u4ee5\u7528 map \u8fd4\u56de\u800c\u4e0d\u662f\u76f4\u63a5 return this._events[type] \u662f\u56e0\u4e3a map \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u662f\u6df1\u5ea6\u590d\u5236\uff0c\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u503c\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u6570\u7ec4\u3002this._events[type] \u5219\u8fd4\u56de\u539f\u6570\u7ec4\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u662f\u6d45\u5ea6\u590d\u5236\uff0c\u7a0d\u4e0d\u5c0f\u5fc3\u6539\u53d8\u503c\u4f1a\u5f71\u54cd\u5230\u539f\u6570\u7ec4\u3002\u9020\u6210\u8fd9\u4e2a\u5dee\u5f02\u7684\u5e95\u5c42\u539f\u56e0\u662f\u6570\u7ec4\u662f\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\uff0c\u6d45\u5ea6\u590d\u5236\u53ea\u662f\u6307\u9488\u62f7\u8d1d\u3002\u8fd9\u53ef\u4ee5\u5355\u72ec\u5199\u4e00\u7bc7\u6587\u7ae0\uff0c\u4e0d\u5c55\u5f00\u4e86\u3002<\/p>\n<p style=\"text-align: left\">listenerCount \u8fd4\u56de\u7684\u662f type \u5bf9\u5e94\u7684\u65b9\u6cd5\u7684\u4e2a\u6570\uff0c\u4ee3\u7801\u4e00\u773c\u5c31\u660e\u767d\uff0c\u4e0d\u591a\u8bf4\u3002<\/p>\n<p style=\"text-align: left\">eventNames \u8fd9\u4e2a\u8fd4\u56de\u7684\u662f\u6240\u6709 type \u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u6ca1\u6709\u8fd4\u56de\u7a7a\u6570\u7ec4\uff0c\u5426\u5219\u7528 Object.keys(this._events) \u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n<p style=\"text-align: left\">\u6700\u540e\u7684\u6700\u540e\uff0cexport default EventEmitter \u628a EventEmitter \u5bfc\u51fa\u3002<\/p>\n<p style=\"text-align: left\">\u7ed3\u8bed<\/p>\n<p style=\"text-align: left\">\u6211\u662f\u5148\u770b\u4e86\u4e24\u4e2a\u5e93\u624d\u77e5\u9053\u600e\u4e48\u5199\u7684\uff0c\u5176\u5b9e\u6700\u597d\u7684\u5b66\u4e60\u65b9\u6cd5\u662f\u77e5\u9053 EventEmitter \u662f\u5e72\u4ec0\u4e48\u7528\u7684\u4ee5\u540e\u81ea\u5df1\u52a8\u624b\u5199\uff0c\u5199\u5b8c\u4ee5\u540e\u518d\u548c\u90a3\u4e9b\u5e93\u8fdb\u884c\u5bf9\u6bd4\uff0c\u627e\u51fa\u5dee\u8ddd\uff0c\u4fee\u6b63\u518d\u4fee\u6b63\u3002<\/p>\n<p style=\"text-align: left\">\u4f46\u4e5f\u4e0d\u662f\u8bf4\u5148\u770b\u518d\u5199\u6ca1\u6709\u6536\u83b7\uff0c\u81f3\u5c11\u6bd4\u53ea\u770b\u4e0d\u5199\u548c\u770b\u90fd\u6ca1\u770b\u7684\u6709\u6536\u83b7\u4e0d\u662f\u3002\u3002\u3002<\/p>\n<p style=\"text-align: left\">\u6c34\u5e73\u6709\u9650\uff0c\u4ee3\u7801\u9519\u6f0f\u6216\u8005\u6587\u7ae0\u8bb2\u4e0d\u6e05\u695a\u4e4b\u5904\u5728\u6240\u96be\u514d\uff0c\u6b22\u8fce\u5927\u5bb6\u6279\u8bc4\u6307\u6b63\u3002<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765JS\u7684EventEmitter\u4f7f\u7528\u6b65\u594f\u8be6\u89e3\uff0c\u4f7f\u7528EventEmitter\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 2\u4e2a\u591a\u6708\u524d\u628a Github \u4e0a\u7684 eventemitter3 \u548c \u4e0b\u7684\u4e8b\u4ef6\u6a21\u5757 events \u7684\u6e90\u7801\u6284\u4e86\u4e00\u904d\uff0c\u624d\u7ec8\u4e8e\u5bf9 \u4e8b\u4ef6\u6709\u6240\u4e86\u89e3\u3002 \u4e0a\u4e2a\u5468\u672b\u82b1\u70b9\u65f6\u95f4\u6839\u636e\u4e4b\u524d\u770b\u6e90\u7801\u7684\u7406\u89e3\u81ea\u5df1\u7528 ES6 \u5b9e\u73b0\u4e86\u4e00\u4e2a eventemitter8\uff0c\u7136\u540e\u4e5f\u53d1\u5e03\u5230 npm \u4e0a\u4e86\uff0c\u8ba9\u6211\u6bd4\u8f83\u610f\u5916\u7684\u662f\u624d\u53d1\u5e03\u4e24\u5929\u5728\u6ca1\u6709 readme \u4ecb\u7ecd\uff0c\u6ca1\u6709\u4efb\u4f55\u5ba3\u4f20\u7684\u60c5\u51b5\u4e0b\u5c45\u7136\u670945\u4e2a\u4e0b\u8f7d\uff0c\u6211\u5f88\u597d\u5947\u90fd\u662f\u8c01\u4e0b\u8f7d\u7684\uff0c\u4f1a\u4e0d\u4f1a\u7528\u3002\u6211\u82b1\u4e86\u4e0d\u5c11\u65f6\u95f4\u534a\u6284\u534a\u539f\u521b\u7684\u4e00\u4e2a JavaScript \u65f6\u95f4\u5904\u7406\u5e93 now.js (npm \u4f20\u9001\u95e8\uff1anow.js) \uff0c\u5728\u6211\u5927\u529b\u5ba3\u4f20\u7684\u60c5\u51b5\u4e0b\uff0c4\u4e2a\u6708\u7684\u4e0b\u8f7d\u91cf\u624d177\u3002\u771f\u662f\u6709\u5fc3\u683d\u82b1\u82b1\u4e0d\u5f00\uff0c\u65e0\u5fc3\u63d2\u67f3\u67f3\u6210\u836b\uff01 eventemitter8 \u5927\u90e8\u5206\u662f\u6211\u6839\u636e\u770b\u6e90\u7801\u7406\u89e3\u540e\u5199\u51fa\u6765\u7684\uff0c\u6709\u4e00\u4e9b\u65b9\u6cd5\u5982listeners\uff0clistenerCount \u548c eventNames \u4e00\u4e0b\u5b50\u60f3\u4e0d\u8d77\u6765\u5230\u5e95\u505a\u4ec0\u4e48\uff0c\u56de\u5934\u91cd\u67e5\u3002\u6d4b\u8bd5\u7528\u4f8b\u4e0d\u5c11\u662f\u53c2\u8003\u4e86 eventemitter3\uff0c\u5728\u6b64\u5bf9 eventemitter3 \u7684\u5f00\u53d1\u8005\u4eec\u548c Node.js \u4e8b\u4ef6\u6a21\u5757\u7684\u5f00\u53d1\u8005\u4eec\u8868\u793a\u611f\u8c22\uff01 \u4e0b\u9762\u6765\u8bb2\u8bb2\u6211\u5bf9 JavaScript \u4e8b\u4ef6\u7684\u7406\u89e3\uff1a \u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0cJavaScript \u4e8b\u4ef6\u6700\u6838\u5fc3\u7684\u5305\u62ec\u4e8b\u4ef6\u76d1\u542c (addListener)\u3001\u4e8b\u4ef6\u89e6\u53d1 (emit)\u3001\u4e8b\u4ef6\u5220\u9664 (removeListener)\u3002 \u4e8b\u4ef6\u76d1\u542c(addListener) \u9996\u5148\uff0c\u76d1\u542c\u80af\u5b9a\u8981\u6709\u76d1\u542c\u7684\u76ee\u6807\uff0c\u6216\u8005\u8bf4\u662f\u5bf9\u8c61\uff0c\u90a3\u4e3a\u4e86\u8fbe\u5230\u533a\u5206\u76ee\u6807\u7684\u76ee\u7684\uff0c\u540d\u5b57\u662f\u4e0d\u53ef\u5c11\u7684\uff0c\u6211\u4eec\u5b9a\u4e49\u4e3a type\u3002 \u5176\u6b21\uff0c\u76d1\u542c\u7684\u76ee\u6807\u4e00\u5b9a\u8981\u6709\u67d0\u79cd\u52a8\u4f5c\uff0c\u5bf9\u5e94\u5230 JavaScript \u91cc\u5b9e\u9645\u4e0a\u5c31\u662f\u67d0\u79cd\u65b9\u6cd5\uff0c\u8fd9\u91cc\u5b9a\u4e49\u4e3a fn\u3002 \u8b6c\u5982\u53ef\u4ee5\u76d1\u542c\u4e00\u4e2a type \u4e3a add\uff0c\u65b9\u6cd5\u4e3a\u67d0\u4e00\u4e2a\u53d8\u91cf a \u503c\u52a01\u7684\u65b9\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31465","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31465"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31465\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}