ECMAScript 5's Section 13.2.2 (on the [[Construct]]
internal property) has this to say about the return value of a constructor:
1) Let obj
be a newly created native ECMAScript object.
...
8) Let result
be the result of calling the [[Call]]
internal property of F
, providing obj
as the this
value and providing the argument list passed into [[Construct]]
as args.
9) If Type(result)
is Object
then return result
.
10) Return obj
.
Thus, the return value of a constructor can only be an object. A string primitive like "foo"
has a Type
result of String
rather than Object
. This means that step 9 is false, so step 10 returns the constructed object, instead of the return value of the constructor function.
Instead, you must return an object (new String("foo")
), as detailed in RobH's answer.