我看到Object.gePrototypeOf(someObject)
返回传递对象的原型,如果aPrototype
是someObject
的原型,则aPrototype.isPrototypeOf(someObject)
返回true。显然,如果Object.getPrototypeOf(someObject)
返回一个名为aPrototype
的原型,那么aPrototype.isPrototypeOf(someObject)
将返回true。但这在我的代码中没有发生:
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
怎么了?
发布于 2016-02-06 09:23:22
根据MDN,isPrototype
的语法是
prototypeObj.isPrototypeOf(obj)
也请参考isPrototypeOf与实例
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));
发布于 2016-02-06 09:19:53
arun
的原型不是person
,而是person.prototype
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
发布于 2016-02-06 09:25:09
isPrototypeOf
不是对构造函数本身调用的,而是在构造函数的prototype属性上调用的。
alert(person.prototype.isPrototypeOf(arun)); // true
这意味着arun的原型不是person
,而是person.prototype
。
https://stackoverflow.com/questions/35243943
复制相似问题