re: Snippets for Vanilla JS Coding VIEW POST

VIEW FULL DISCUSSION
 

Perhaps a bit excessive to attach the on method to the Array prototype but it does the trick =)

 

Another option is to attach the on method plus Array.prototype.map to the NodeList prototype instead:

const $$ = (selector, startNode = D) => startNode.querySelectorAll(selector)
NodeList.prototype.map = Array.prototype.map
NodeList.prototype.on = function(type, listener, options) {
  this.map(el => {
    if (el instanceof Element) {
      el.addEventListener(type, listener, options)
    }
  })
  return this // for chaining
}

This keeps the Array prototype clean =).

 

Would it be good to have passive listener by default if no options are passed?

if (options == null) options = {passive: true};
code of conduct - report abuse