Literal constructor

[literal-constructor] Do not use function as a constructor.

This warning category is spelled [literal-constructors] by qmllint.

Do not use function as a constructor

What happened?

A literal construction function was used as a constructor.

Why is that bad?

Calling a literal construction function such as Number as a regular function coerces the passed value to a primitive number. However, calling Number as a constructor returns an object deriving from Number containing that value. This is wasteful and likely not the expected outcome. Moreover, it may lead to unexpected or confusing behavior because of the returned value not being primitive.

Example

To fix this warning, do not call these functions as constructors but as regular functions:

Array has confusing semantics

What happened?

An Array constructor was used with more than one argument.

Why is that bad?

The Array constructor has confusing semantics:

  • new Array() creates an empty array.

  • new Array(42) creates an array with 42 elements.

  • new Array(42, 43) creates an array with two elements, 42 and 43.

Example

To fix this warning, use [...] instead of new Array(...).

functions: