I've done some tests and I'm wondering why a new TestOne()
evaluates to a new instance of the function but a new TestThree()
evaluates to the return value.
It appears that this depends on whether an object is returned or a primitive type. I'd expect new TestThree()
to evaluate to TestThree {}
(an instance of it). What happens to the instance of TestThree
that's created by new
?
What are the rules behind this and why?
function TestOne() {
return 123;
}
new TestOne(); // TestOne {}
TestOne(); // 123
function TestTwo() {
}
new TestTwo(); // TestTwo {}
TestTwo(); // undefined
function TestThree() {
return new TestOne();
}
new TestThree(); // TestOne {}
TestThree(); // TestOne {}