Search code examples
javascriptprototypal-inheritance

Object.getPrototypeOf and example.isPrototypeOf(obj) gave confusing results


I read that Object.gePrototypeOf(someObject) returns the prototype of the passed object and aPrototype.isPrototypeOf(someObject) returns true if aPrototype is the prototype of someObject. It is obvious to me that if Object.getPrototypeOf(someObject) returns a prototype named aPrototype, then aPrototype.isPrototypeOf(someObject) will return true. But it's not happening in my code:

function person(fname, lname)
{
  this.fname = fname;
  this.lname = lname;   
}

var arun = new person('Arun', 'Jaiswal');

console.log(Object.getPrototypeOf(arun));  //person
console.log(person.isPrototypeOf(arun));   //false

What's wrong?


Solution

  • As per MDN, syntax of isPrototype is

    prototypeObj.isPrototypeOf(obj)
    

    Also refer isPrototypeOf vs instanceof

    function person(fname, lname)
    {
      this.fname = fname;
      this.lname = lname;   
    }
    
    var arun = new person('Arun', 'Jaiswal');
    
    console.log(Object.getPrototypeOf(arun));  //person
    console.log(person.prototype.isPrototypeOf(arun));