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?
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));
Prototype of arun
is not person
but person.prototype
:
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
isPrototypeOf
is not called on the constructor itself, but on the constructor's prototype property.
alert(person.prototype.isPrototypeOf(arun)); // true
This means that the prototype of arun is not person
, but person.prototype
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.