I'm trying to find a good explanation why new foo() instanceof foo returns false.
function foo() {
return foo;
}
new foo() instanceof foo;
If the function foo was defined as, it returns true as expected
function foo(){
return 1;
}
I'm trying to find a good explanation why new foo() instanceof foo returns false.
function foo() {
return foo;
}
new foo() instanceof foo;
If the function foo was defined as, it returns true as expected
function foo(){
return 1;
}
The latter doesn't really return 1
as constructor functions are not allowed to return value types. The return value is ignored and what you get is a new foo
(rather than 1
) which of course is of type foo
.
On the other hand, the former returns a function itself which is of type Function
.
function foo() {
return foo;
}
console.log( new foo() instanceof Function );
// true
function bar() {
return 1;
}
console.log( new bar() );
// prints {} as new bar() returns an empty object of bar proto