Might this not be a case of solving the wrong problem? Wouldn't it be
possible to chose an Identifier for use in place of 'o' that told the
reader of the code enough about the value that the variable was
expected to hold that they did not need to look any further unless
debugging showed evidence that the actual value was not consistent
with the expectation?
<snip>
An object, but not necessarily the object that resulted from - new -
operation, and possibly a function, Date, RegExp, String, host, etc.
object. The use of the - new - operator really only grantees that the
value will not be a primitive, which isn't such a great step forward.
With `new`, the invocation is at the beginning of an expression so it
is immediately apparent. With a CallExpression it is at the end.
var animalBuilder = new function() {
/*...*/
};
As you pointed out, a constructor may have an explicit return
statement and return an object. But if it is used with a
NewExpression, why would it?
An anonymous function might have any reason for not being called
immediately. There is a lot of code that does things like that and for
good reason. For example:
var animalBuilder = function() {
/*...*/
};
A NewExpression with a FunctionExpression is perfectly valid and fine
to use. The only problem is confusion, which has been demonstrated by
the misconceptions that have been posted. Those misconceptions are:
1) it might not return an object
2) it is an awful choice
3) it cannot accept Arguments
1) False. A new expression will always result in an object being
created and returned unless the function throws an error.
2) In the right context, it is a good option
3) False, as shown
It is confusing to beginners. Like anything, there is potential for
misuse. One type of misuse is where only a closure is needed and the
code does not assigning to `this` and discards the result.
Using new expression with function expression is fine. Once the
mechanics are understood (and they really are quite simple), the
pattern should not be confusing.