Last active
June 17, 2017 05:08
-
-
Save lqt0223/33195931b223f9db961c3db78dc3aaa9 to your computer and use it in GitHub Desktop.
Event System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function EventEmitter () { | |
| var eventStorage = {} | |
| this.on = function(event, handler) { | |
| if(!eventStorage[event]) eventStorage[event] = [] | |
| eventStorage[event].push(handler) | |
| } | |
| this.emit = function(event) { | |
| var events = eventStorage[event] | |
| for (var i = 0; i < events.length; i++) { | |
| var handler = events[i] | |
| var args = Array.prototype.slice.call(arguments, 1) | |
| handler.apply(this,args) | |
| } | |
| } | |
| } | |
| // test | |
| var a = new EventEmitter() | |
| a.on("test",function(a,b,c) { | |
| console.log("test", a,b,c); | |
| }) | |
| a.emit("test", 1,2,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment