Is it bad practice to not assign a new object to a variable?

"Is it bad javascript practice to not assign a newly created object to a variable if you're never going to access it?"

I feel it is bad practice to make an assignment that is not needed, and, I would argue, not just for javascript but in general. If there are side-effects you want, getting them from the action of an assignment is bad practice for the simple reason that it would be fairly opaque from a maintenance point of view.

.


If you're not accessing it but it's still useful, that suggests that the constructor itself has visible side effects. Generally speaking, that's a bad idea.

What would change if you didn't call the constructor at all?

If your constructor is doing something to the global state, that strikes me as very bad. On the other hand, you could be using it just for the sake of validation - i.e. if the constructor returns without throwing an exception, it's okay. That's not quite so bad, but a separate method for validation would make things a lot clearer if that's the case.


That’s absolutely fine if you don’t need to use it again.

Tags:

Javascript