All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S2999.html Maven / Gradle / Ivy

There is a newer version: 10.20.0.29356
Show newest version

Why is this an issue?

JavaScript has the new keyword that is used in conjunction with constructor functions to create new instances of objects. When you use the new keyword with a function, it signifies that the function is intended to be used as a constructor function to create objects.

Any function can be used as a constructor function by convention. Constructor functions are used to create new objects with the same structure or properties. They are typically named with an initial capital letter to distinguish them from regular functions.

To create a new instance of an object using the constructor function, you use the new keyword before the function call.

The new keyword should only be used with objects that define a constructor function. Attempting to use it with an object or a variable that is not a constructor will raise a TypeError.

function MyClass() {
  this.foo = 'bar';
}

const someClass = 1;

const obj1 = new someClass;    // Noncompliant: someClass is a variable
const obj2 = new MyClass();    // Noncompliant if parameter considerJSDoc is true. Compliant when considerJSDoc is false

Always use the new keyword with constructor functions or classes.

/**
 * @constructor
 */
function MyClass() {
  this.foo = 'bar';
}

const someClass = function(){
  this.prop = 1;
}

const obj1 = new someClass;
const obj2 = new MyClass();

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy