I was wondering if pure classes make sense or not (as a concept)?
For example the constraints would be:
No inheritance (only composition)
All dependencies are passed in the constructor, or methods
For example:
class Elem {
constructor(el) {
this.el = el;
}
get html() {
return this.el.innerHTML;
}
get cls() {
return this.el.className.split(' ');
}
set cls(arr) {
this.el.className(arr.join(' '));
}
hide() {
this.el.style.display = 'none';
}
}
const appEl = document.getElementById('app');
const app = new Elem(appEl);
app.cls = ['flex', 'h100'];
app.hide();
Just wondering if having such constraints would add benefits similar to pure functions?
Update
I think the above example is bad, because this.el
lives somewhere else I guess (in the dom), but what about:
class One {
constructor() {
this.val = 1;
}
add(val) {
return this.val + val;
}
}
const one = new One;
one.add(6);