In my coding, I often utilize a design pattern similar to the concept of custom objects.
However, JSLint disapproves of code constructs like this:
function MyClass() { this.init(); }
new MyClass(data);
The reason being that the newly created object is discarded without any further use. Even though we can trick JSLint by assigning it to a variable, the underlying notion remains unchanged - JSLint along with many JavaScript enthusiasts frown upon this practice.
So why exactly is incorporating side effects in a JavaScript constructor considered bad?
On a positive note, I perceived this as a beneficial approach because:
- It simplifies the setup process since there is only one function, making it easier to maintain when managing multiple instances of MyClass for future access. (Adding an object to an array after the constructor returns would be viewed as a side effect and hence not recommended = more challenging to maintain)
- Each instance has its own prototype, signifying a sense of "class ownership": Firebug identifies it as an instance of MyClass rather than just Object. (In my perspective, this makes it more superior compared to other design patterns.)