V
V
Vitaliy2015-08-12 18:06:40
JavaScript
Vitaliy, 2015-08-12 18:06:40

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);
};

Or is it worth creating separate methods for binding events?
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

1 answer(s)
E
Evgeniy Kvasyuk, 2015-08-12
@EvgeniyKvasyuk

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 question

Ask a Question

731 491 924 answers to any question