In normal cases, new
should be used when creating objects from a constructor function and eschewed when performing any other function call. When using new
on a function 1) a new object is created; 2) the function is called with the value of this
bound to that new object, and 3) the value returned from the function (by default) is the object created in step one.
However, in your case you're returning a completely new object from the "constructor" (different from the object in 1) above), which means there is no practical difference. The value of this
will still be different inside the function, but both of these will return false
:
new Foo() instanceof Foo;
Foo() instanceof Foo;
To illustrate the this
difference, add the following to Foo
:
alert(this instanceof Foo)
When called with new
this alerts true
; when called without it, false
.
Furthermore, there's no point in assigning an object to Foo.prototype
because you'll never create any instances of Foo
that would make use of it (because, again, you're returning something completely different from Foo
).
If you're going to return a custom object from Foo
then you should prefer the version without new
; there's no point in creating a new instance of Foo
if you're going to ignore it and return something completely different, anyway.