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();
try block is ignored.
try {
new MyConstructor();
} catch (e) {
/* ... */
}
Notification or Vue are also ignored.Construct class of the constructs package and the scope passed as its first argument derives from
Construct or implements IConstruct. The IConstruct interface is accepted only for scope, not for
the instantiated class. A construct registers itself in the construct tree of that scope during construction, so the created object
does not need to be retained.
import { Construct } from 'constructs';
class Bucket extends Construct {
constructor(scope, id) {
super(scope, id);
}
}
class MyConstruct extends Construct {
constructor(scope, id) {
super(scope, id);
new Bucket(this, 'Bucket'); // Compliant: registered in this construct's tree
}
}
RegExp constructor used only to validate dynamic regex syntax before returning the parsed values are also
ignored.
function parseRegExp(regExp) {
const patternEnd = regExp.lastIndexOf("/");
const pattern = regExp.slice(1, patternEnd);
const flags = regExp.slice(patternEnd + 1);
new RegExp(pattern, flags);
return { pattern, flags };
}
const canvas = document.querySelector('#chart');
const context = canvas.getContext('2d');
new Chart(context);
Object.prototype.constructornew operator