
rules.jshint.W055.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
When do I get this error?
The "A constructor name should start with an uppercase letter" error is thrown
when JSLint, JSHint or ESLint encounters an identifier, preceded by the new
operator, whose first character is a lowercase letter. JSHint will only raise
this warning when the newcap
option is set to true
. In the following
example we declare a constructor function myConstructor
and then attempt to
instantiate it:
x 1/*jshint newcap: true */
2function myConstructor() {
3 "use strict";
4 this.property = "Something";
5}
6
7var myInstance = new myConstructor();
8
Why do I get this error?
This error is raised to highlight a lack of convention. It is common practice for constructor function identifiers to begin with an uppercase letter. JSLint simply enforces this convention. Here's the above snippet rewritten to pass JSLint. Notice that the only difference is the uppercase "M":
8 1/*jshint newcap: true */
2function MyClass() {
3 "use strict";
4 this.property = "Something";
5}
6
7var myInstance = new MyClass();
8
It is worth bearing in mind that this is only a convention and is not required
by the language in any way. You can safely ignore this error if you prefer to
name your constructor functions differently. By setting the newcap
option, you
can tell JSLint to allow lowercase constructors.
In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. The identifier of this warning is W055.
This means you can tell JSHint to not issue this warning with the /*jshint
-W055 */
directive.