Object.getPrototypeOf和example.isPrototypeOf(obj)给出了令人困惑的结果

时间:2016-02-06 17:16:58

标签: javascript prototypal-inheritance

我读到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

出了什么问题?

3 个答案:

答案 0 :(得分:3)

根据MDNisPrototype的语法是

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

相关问题