Reference B station video: www.bilibili.com/video/BV1o4…

1. Publish and subscribe

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, < frame id=" BTN "onclick="send"> </ frame > </ frame > </html> <script> function Events() { this.callbacks = []; this.results = []; } Events.prototype.on = function(callback) { this.callbacks.push(callback); }; // events.prototype. Emit = function(data) {this.results.push(data); this.callbacks.forEach((i) => i(this.results)); }; // publish let e = new Events(); e.on(function(data) { console.log(data); }); console.log(e); let btn = document.getElementById("btn"); Btn.onclick = function() {e.mit (" 中 国 "); }; </script>Copy the code

(1) When I subscribe, I decide what I need to do, pass in a callback function, and then the subscribe function will pass in the callback function, in an array, this array holds all of our subscribe events;

(2) release: we need when we release to us to publish news, the news is to be deposited into an array of results, because more than could be released, and then when we release, we need to put all subscribe to the event, and then introduced to all of our data to them;