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;
}
Jeremy
  • 11
  • 1
  • maybe this will help: https://stackoverflow.com/questions/23622695/why-in-javascript-both-object-instanceof-function-and-function-instanceof-obj – Zombie Jun 03 '18 at 17:24
  • It might help to do `const instance = new foo; console.log(instance, instance instanceof foo)`. – Bergi Jun 03 '18 at 18:17

1 Answers1

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
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106