我读到Object.gePrototypeOf(someObject)
返回传递的对象的原型,如果aPrototype.isPrototypeOf(someObject)
是aPrototype
的原型,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
出了什么问题?
答案 0 :(得分:3)
根据MDN,isPrototype
的语法是
prototypeObj.isPrototypeOf(obj)
另请参阅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));

答案 1 :(得分:2)
arun
的原型不是person
,而是person.prototype
:
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
答案 2 :(得分:1)
replace="s|/home/bamboo/project/|`pwd`/|g"
sed -i -- $replace report/*.xml
不是在构造函数本身上调用的,而是在构造函数的原型属性上调用的。
isPrototypeOf
这意味着arun的原型不是alert(person.prototype.isPrototypeOf(arun)); // true
,而是person
。