Steven D'Aprano <steve@pearwood.info> writes:
>What's the difference between calling a constructor with or
>without new?
With new, in the constructor, »this« gets assigned to a new
object that has the function as its constructor and the
function's prototype as its internal prototype.
A function can detect whether it was possibly invoked as
a constructor.
For example,
function Alpha( arg )
{ "use strict"; if( !( this instanceof Alpha )) ... else ... }
.
One can use this to align the function's behavior with the
constructor:
function Alpha( arg )
{ "use strict";
if( !( this instanceof Alpha ))return new Alpha( arg ); else ... }
In the standard library, several constructors can also be
called as functions, albeit sometimes with different semantics.
In strict mode, »this« is not defined in function calls,
so erroneously forgetting »new« will be detected more often.