Last active
June 4, 2018 11:35
-
-
Save ZaneHannanAU/759f57a831ed16c94d35e4156da5c33c to your computer and use it in GitHub Desktop.
Event listener add multiple simultaneous.
This file contains 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
Object.defineProperty(self || this, 'addEventListeners', { | |
value: function addEventListeners(target, listeners, opts = {}) { | |
if (listeners === undefined) | |
return addEventListeners(this, target, opts) | |
if ('function' === typeof opts.init) opts.init.call(target) | |
if (opts.addSelf) Object.defineProperty(target, 'string' === typeof opts.addSelf ? opts.addSelf : addEventListeners.name, { | |
enumerate: true, | |
value: addEventListeners | |
}) | |
for (const listener in listeners) | |
if (listeners.hasOwnProperty(listener)) | |
target.addEventListener(listener, listeners[listener], opts) | |
return {target, listeners, opts} | |
}, | |
enumerable: true | |
}) |
This file contains 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
let j = addEventListers({ | |
popstate(pop) { | |
console.log(pop) | |
}, | |
load() { | |
console.log('load fire') | |
addEventListeners(document, {}, {addSelf: 'on'}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment