I use something quite similar to the design pattern custom objects in my code normally.
But JSLint frowns upon constructs like this:
function MyClass() { this.init(); }
new MyClass(data);
Because the object is being discarded immediately after creation - it isn't being used for anything. We can fool JSLint to ignore this by assigning it to a variable, but it doesn't change that JSLint (and I am guessing many JavaScript enthusiasts) discourages the pattern.
So why is using side effects in a JavaScript constructor seen as a bad practice?
For what it's worth, I thought this was a good practice because:
In his book Clean Code, Robert Martin says
Side effects are lies. Your function promises to do one thing, but it also does other hidden things...they are devious and damaging mistruths that often result in strange temporal couplings and order dependencies.
What you described in your comment regarding arrays sounds like a "strange temporal coupling".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With