-
什么是 isPrototypeOf 属性
-
isPrototypeOf 用于判断 一个对象是否是另一个对象的原型
class Person{ name = "lnj"; } let p = new Person(); // 判断Person.prototype对象是否是p实例对象的原型 console.log(Person.prototype.isPrototypeOf(p)); // true class Cat{ name = "mm"; } let c = new Cat(); // 判断Cat.prototype对象是否是p实例对象的原型 console.log(Cat.prototype.isPrototypeOf(p)); // false
-
-
isPrototypeOf注意点
-
只要构造函数的原型对象出现在实例对象的原型链中都会返回 true
function Person(myName) { this.name = myName; } function Student(myName, myScore) { Person.call(this, myName); this.score = myScore; } Student.prototype = new Person(); Student.prototype.constructor = Student; let stu = new Student(); // Person原型对象在stu实例对象的原型链中, 所以会返回true console.log(Person.prototype.isPrototypeOf(stu)); // true
-
网友评论