The root of your confusion is the way that Chrome represents objects on its console.
The expression new f()
in your example is represented as 'f
' on the Chrome's console output, but just as a mere "convenience" of this particular console, for example, logging the following object:
({constructor: function Foo(){}});
Will show it represented as "Foo
". Basically the console tries to find a representation of which constructor your object is instance of.
So, the console shows you f
, but you are not logging the function, the new
operator produces an object that inherits from f.prototype
.
You are returning 10
from the function, but since you call it with new
, and the return type is a primitive, the value is discarded, the newly created object that inherits form f.prototype
is returned.
For example:
var F = function () { return 10; };
F.prototype.inherited = 'foo';
var h = new F(); // the 10 returned is ignored
h instanceof F; // true
h.inherited; // "foo"
On the other hand, if you return an object from a constructor, the object instance that the new
operator creates behind the scened (and inherits from the constructor's prototype) will be lost, for example:
var F = function () { return {}; };
F.prototype.inherited = 'foo';
var h = new F(); // the empty object is returned
h instanceof F; // false
h.inherited; // undefined
Yes, new f;
and new f();
are completely equivalent, although people recommend using the parentheses to add clarity to your code.