Creating an object without assigning it to a variable or using it in any function means the object is essentially created for no reason and may be
dropped immediately without being used. Most of the time, this is due to a missing piece of code and could lead to an unexpected behavior.
If it’s intended because the constructor has side effects, that side effect should be moved into a separate method and called directly. This can
help to improve the performance and readability of the code.
new MyConstructor(); // Noncompliant: object may be dropped
Determine if the objects are necessary for the code to function correctly. If they are not required, remove them from the code. Otherwise, assign
them to a variable for later use.
let something = new MyConstructor();
Exceptions
- Creating new objects inside a
try
block is ignored.
try {
new MyConstructor();
} catch (e) {
/* ... */
}
- Known constructors with side effects like
Notification
or Vue
are also ignored.