+1 TJ Crowder understands it well. The ECMAScript standard specifically outlines the behaviors of built-in constructor functions when called as regular functions. There are times when the constructor simply calls itself back, but there are also instances that are more complex.
Constructors in JavaScript [...] should not be expected to return anything
Typically, a constructor can disregard this
and instead return an independent object:
function Thing() {
return {'foo': 1};
}
In such cases, you can treat the function both as a constructor (using new
) or a regular function.
If a constructor does not return anything, which is common for constructors, the new
operator automatically ensures that it returns the newly created object passed as this
. In this scenario, you have to use new
.
Relying on a constructor to function like a plain function is not recommended, and the alternative behaviors of built-in constructors are seldom useful. It's generally advised to stick with using new
.