{"id":272,"date":"2021-07-01T16:13:22","date_gmt":"2021-07-01T08:13:22","guid":{"rendered":"http:\/\/gjweb.top\/?p=272"},"modified":"2021-07-01T16:13:23","modified_gmt":"2021-07-01T08:13:23","slug":"%e6%89%8b%e5%86%99eventbus","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=272","title":{"rendered":"\u624b\u5199EventBus"},"content":{"rendered":"\n<p>EventBus \u53c8\u53eb\u505a \u4e8b\u4ef6\u603b\u7ebf\u4f20\u503c ,\u672c\u8d28\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u5b9e\u73b0\uff0c<\/p>\n\n\n\n<p>\u89c2\u5bdf\u8005\u6a21\u5f0f\u5c31\u662f \u5b9a\u4e49\u4e86\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\uff0c\u8ba9\u591a\u4e2a\u89c2\u5bdf\u8fd9\u5bf9\u8c61\u540c\u4e8b\u76d1\u542c\u67d0\u4e00\u4e2a\u4e3b\u9898\u5bf9\u8c61\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6240\u6709\u4f9d\u8d56\u4e8e\u4ed6\u7684\u5bf9\u8c61\u5c06\u5f97\u5230\u901a\u77e5<\/p>\n\n\n\n<p>\u4ed6\u5728 Node \u4e2d\u7684\u4f7f\u7528\uff0con \u662f\u7528\u6765\u76d1\u542c\u4e8b\u4ef6 \uff0c emit \u662f\u7528\u6765\u89e6\u53d1\u4e8b\u4ef6 \uff0c \u4e00\u65e6emit\u89e6\u53d1\u4e86\u4e8b\u4ef6\uff0con\u5c31\u4f1a\u88ab\u76d1\u542c\u5230\uff0c\u4ece\u800c\u89e6\u53d1\u56de\u8c03\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const EventEmitter = require('events');<br>class MyEmitter extends EventEmitter {}<br>const myEmitter = new MyEmitter();<br>myEmitter.on('\u55e8', (str) =&gt; {<br> console.log(str);<br>});<br>myEmitter.emit('\u55e8','\u4f60\u597d');<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u624b\u5199\u5b9e\u73b0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class EventBus {<br> &nbsp; &nbsp;constructor(){<br> &nbsp; &nbsp; &nbsp; &nbsp;this.eventList = {}<br> &nbsp;  }<br> &nbsp; &nbsp;on(eventName,eventCallBack){<br> &nbsp; &nbsp; &nbsp; &nbsp;if(this.eventList&#91;eventName]){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.eventList&#91;eventName].push(eventCallBack)<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.eventList&#91;eventName] = &#91;eventCallBack]<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;emit(eventName,...params){<br> &nbsp; &nbsp; &nbsp; &nbsp;if(this.eventList&#91;eventName]){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.eventList&#91;eventName].forEach((item,index)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.apply(this,params)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u4f7f\u7528<br>let event = new EventBus()<br>event.on(\"hello\",(params)=&gt;{<br> &nbsp; &nbsp;console.log(\"\u76d1\u542c\u5230\u4e86\"\uff0cparams)<br>})<br>event.emit(\"hello\",\"eventBus\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>EventBus \u53c8\u53eb\u505a \u4e8b\u4ef6\u603b\u7ebf\u4f20\u503c ,\u672c\u8d28\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u5b9e\u73b0\uff0c \u89c2\u5bdf\u8005\u6a21\u5f0f\u5c31\u662f \u5b9a\u4e49\u4e86\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9\u591a\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=272"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}