0

I have an HTMLCollection: document.getElementsByClassName('done') and now i want to implement a function addEventListeners.Now i can do it like this:

Object.prototype.addEventListeners = function(argument){ Object.values(this).forEach(function(element, index) { }); }; 

But i have read in numerous websites that extending native objects is a bad idea.So what are the alternatives?

10
  • why don't use a framework like JQuery or AngularJS ? Commented Jan 30, 2018 at 8:25
  • @FarukT i'm doing this for a better understanding of JS Commented Jan 30, 2018 at 8:25
  • 1
    As an alternate, you can attach a delegate to parent, if they have same parent or you can loop over on elements. You can use Array.from(elementList, (el)=> { el.addEventListener(evenName, handler)}) Commented Jan 30, 2018 at 8:32
  • @str i'm not asking why it's bad,i'm asking for alternatives Commented Jan 30, 2018 at 8:35
  • 1
    If you extend natives, you should use HTMLCollection in this case Commented Jan 30, 2018 at 8:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.