When I use new
to instainciate an instance of a certain class, I got the actual instance. When the constructor function has a return value, the new
sentence gives out the actual instance also. However, when the constructor returns itself, I can't get the instance. Instead I get the constructor. I wander what's wrong with this.
Here are my test code fragment:
function foo() {
this.x = 1;
return foo;
}
console.log(new foo()); // prints the defination of foo
As we consider, in most situations, it makes no sence to return a function like this. However, why does JS has such a feature? Is there any consideration when designing JS? Or is it just a bug of JS?