Expand on constructor guidelines
In point 2 of https://docs.gitlab.com/ce/development/fe_guide/style_guide_js.html#data-mutation-and-pure-functions we say "Avoid constructors with side-effects" but don't offer an example.
We should offer an example.
Maybe...
2. Avoid constructors with sideeffects
A constructor should only be responsible for preparing an instance of a class to be used.
It should not perform any actions or invoke any behaviour.
// bad
class ClickHandler {
constructor(element) {
this.element = element;
this.bindEvents();
}
bindEvents() {
this.element.addEventListener('click', () => {});
}
}
const element = document.getElementById('my-element');
new ClickHandler(element);
// good
class ClickHandler {
constructor(element) {
this.element = element;
}
bindEvents() {
this.element.addEventListener('click', () => {});
}
}
const element = document.getElementById('my-element');
const clickHandler = new ClickHandler(element);
clickHandler.bindEvents();