0

I started learning JavaScript and have below question:

var f = function foo() {}
Console.log(f.__proto__ === Function.prototype) //true 
Console.log(f.__proto__ instance of Function) //false

Why 3rd statement using instanceof returns false. My understanding is RHS of instance consults prototype of passed class and then match it in object or its proto. Please let me know what I am missing here? referred this for implementation of instance-of.

4
  • 2
    You have syntax errors: instanceof is one word, and console must be lowercase. Commented Apr 21, 2018 at 8:21
  • you can refer JavaScript Commented Apr 21, 2018 at 8:48
  • related, if not duplicate: Why is Object.prototype instanceof Object false? Commented Apr 21, 2018 at 8:54
  • yes, it's typo instanceof. Commented Apr 21, 2018 at 9:30

1 Answer 1

0

referred this for implementation of instance-of

Well, that's simply a wrong implementation. The instanceof operator does not match the object itself, only its prototypes, against the .prototype of the constructor. Your

x instanceof Function

is equivalent to

Function.prototype.isPrototypeOf(x)

which for f.__proto__ (i.e. Function.prototype) does not hold - it is not the prototype of itself.

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

3 Comments

Thanks, i have no idea about isprototypeof. will read about that. IN this case f.__proto__ ->Function.prototype; instanceof finds prototype property of RHS i.e. Function.prototype and then tries to find a match. Isn't that correct? If not, is there a way to find implemented code of instanceof.
Yes, it tries to find Function.prototype (the LHS) in the prototype chain of the .prototype of Function (the RHS). But it is not in the chain, it is the start of the chain.
You can read about isPrototypeOf here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.