Object.getPrototypeOf 和 example.isPrototypeOf(obj) 给出了令人困惑的结果
Object.getPrototypeOf and example.isPrototypeOf(obj) gave confusing results
我读到 Object.gePrototypeOf(someObject)
return 是传递对象的原型,如果 aPrototype
是 [= 的原型,aPrototype.isPrototypeOf(someObject)
return 为真16=]。对我来说很明显,如果 Object.getPrototypeOf(someObject)
return 是一个名为 aPrototype
的原型,那么 aPrototype.isPrototypeOf(someObject)
将 return 为真。但这并没有发生在我的代码中:
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
怎么了?
arun
的原型不是 person
而是 person.prototype
:
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
根据 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));
isPrototypeOf
不是在构造函数本身上调用,而是在构造函数的原型 属性 上调用。
alert(person.prototype.isPrototypeOf(arun)); // true
也就是说arun的原型不是person
,而是person.prototype
。
我读到 Object.gePrototypeOf(someObject)
return 是传递对象的原型,如果 aPrototype
是 [= 的原型,aPrototype.isPrototypeOf(someObject)
return 为真16=]。对我来说很明显,如果 Object.getPrototypeOf(someObject)
return 是一个名为 aPrototype
的原型,那么 aPrototype.isPrototypeOf(someObject)
将 return 为真。但这并没有发生在我的代码中:
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
怎么了?
arun
的原型不是 person
而是 person.prototype
:
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
根据 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));
isPrototypeOf
不是在构造函数本身上调用,而是在构造函数的原型 属性 上调用。
alert(person.prototype.isPrototypeOf(arun)); // true
也就是说arun的原型不是person
,而是person.prototype
。