Answer the question
In order to leave comments, you need to log in
JS: Where to bind event handlers in constructor functions?
Where in the code should you add event handlers?
Directly in the constructor itself:
var Func = function (el) {
this.button = document.querySelector(el);
this.button.addEventListener('click', someFunction);
};
var Func = function (el) {
this.button = document.querySelector(el);
};
Func.prototype.init = function () {
this.button.addEventListener('click', someFunction);
this.anotherElement.addEventListener('change', anotherFunction);
};
Answer the question
In order to leave comments, you need to log in
I take out module initiation always in a separate function, which suspends event handlers. So the same type of actions are taken out in a separate function, it turns out more beautiful.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question