Skip to content

Instantly share code, notes, and snippets.

@ZaneHannanAU
Last active June 4, 2018 11:35
Show Gist options
  • Save ZaneHannanAU/759f57a831ed16c94d35e4156da5c33c to your computer and use it in GitHub Desktop.
Save ZaneHannanAU/759f57a831ed16c94d35e4156da5c33c to your computer and use it in GitHub Desktop.
Event listener add multiple simultaneous.
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
})
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