Search code examples
javascriptfunctionprototypeinstanceof

javascript Function.prototype issue


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.


Solution

  • 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.