{"id":1454,"date":"2017-03-18T12:18:29","date_gmt":"2017-03-18T04:18:29","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=1454"},"modified":"2017-03-18T12:18:29","modified_gmt":"2017-03-18T04:18:29","slug":"js%e4%ba%8b%e4%bb%b6%e6%a8%a1%e5%9e%8bbind%e3%80%81unbind%e3%80%81trigger%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=1454","title":{"rendered":"js\u4e8b\u4ef6\u6a21\u578bbind\u3001unbind\u3001trigger\u5b9e\u73b0"},"content":{"rendered":"<pre>function Emitter(){\r\n    this._listener = {}\r\n}\r\nEmitter.prototype.bind = function(eventName,callback){\r\n    var event = this._listener[eventName] || [];\r\n    event.push(callback);\r\n    this._listener[eventName] = event;\r\n}\r\nEmitter.prototype.trigger = function(eventName){\r\n    if(!this._listener.hasOwnProperty(eventName)){\r\n       console.log('you do not bind this event');\r\n       return;\r\n    }\r\n    var args = Array.prototype.slice.apply(arguments).slice(1);\r\n    var event = this._listener[eventName];\r\n    if(!Array.isArray(event)) return;\r\n\r\n    event.forEach(function(callback){\r\n        try{\r\n            callback.apply(this,args)\r\n        }catch(e){\r\n            console.log(error(e))\r\n        }\r\n    })\r\n}\r\nEmitter.prototype.unbind = function(eventName,callback){\r\n    this._listener.hasOwnProperty(eventName) &amp;&amp; delete this._listener[eventName];\r\n    callback &amp;&amp; callback();\r\n}\r\nvar em = new Emitter();\r\nem.bind('test',function(a,b){\r\n    console.log(a);\r\n    console.log(b);\r\n});\r\nem.trigger('test',1,2)\r\nem.unbind('test',function(){console.log('unbind')})\r\nem.trigger('test',1,2)<\/pre>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=1454\">js\u4e8b\u4ef6\u6a21\u578bbind\u3001unbind\u3001trigger\u5b9e\u73b0<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>function Emitter(){ this._listener = {} } Emitter.prototype.bind = function(eventName,callback){ var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,212],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1454"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1454"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1454\/revisions"}],"predecessor-version":[{"id":1455,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1454\/revisions\/1455"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}