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;
}
2
  • maybe this will help: stackoverflow.com/questions/23622695/… Commented Jun 3, 2018 at 17:24
  • It might help to do const instance = new foo; console.log(instance, instance instanceof foo). Commented Jun 3, 2018 at 18:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.