Skip to content

Instantly share code, notes, and snippets.

@skt-t1-byungi
Created August 3, 2018 07:42
Show Gist options
  • Save skt-t1-byungi/751145177ec20af44444e23ffd690f2c to your computer and use it in GitHub Desktop.
Save skt-t1-byungi/751145177ec20af44444e23ffd690f2c to your computer and use it in GitHub Desktop.
eventemitter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
hello
<script>
! function(t, e) {
"object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : t.EventEmitter = e()
}(this, function() {
"use strict";
function t(t) {
var e = typeof t;
if ("function" !== e) throw new TypeError("Expected listener to be a function, but " + e)
}
return function() {
function e() {
this._listeners = {}
}
return e.prototype.on = function(e, n) {
t(n), this._hasListeners(e) || (this._listeners[e] = []), this._listeners[e].push(n)
}, e.prototype.off = function(e, n) {
n && t(n), this._hasListeners(e) && (n ? (this._listeners[e] = function(t, e) {
if (t.filter) return t.filter(e);
for (var n = t.length, i = [], r = 0; r < n; r++) e(t[r], r, t) && i.push(t[r]);
return i
}(this._listeners[e], function(t) {
return t !== n && t.key !== n
}), 0 === this._listeners[e].length && delete this._listeners[e]) : delete this._listeners[e])
}, e.prototype.once = function(e, n) {
var i = this;
t(n);
var r = function() {
for (var t = [], r = 0; r < arguments.length; r++) t[r] = arguments[r];
i.off(e, n), n.apply(void 0, t)
};
r.key = n, this.on(e, r)
}, e.prototype.emit = function(t) {
for (var e = [], n = 1; n < arguments.length; n++) e[n - 1] = arguments[n];
this._hasListeners(t) && function(t, e) {
if (t.forEach) return t.forEach(e);
for (var n = t.length, i = 0; i < n; i++) e(t[i], i, t)
}(this._listeners[t].slice(), function(t) {
return t.apply(void 0, e)
})
}, e.prototype._hasListeners = function(t) {
return Object.prototype.hasOwnProperty.call(this._listeners, t)
}, e
}()
});
//# sourceMappingURL=EventEmitter.min.js.map
var emitter = new EventEmitter();
emitter.on('test', function (str) {
alert(str)
})
emitter.emit('test', 'hello')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment