1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Person{ constructor(name, age) { this.name = name; this.age = age; } }
class Student extends Person { constructor(name, age, sex) { super(name, age); this.sex = sex; } } function myInstance(obj1, obj2) { let obj1Proto = obj1.__proto__; while(true) { if (obj1Proto == null) { return false; } if (obj1Proto == obj2.prototype) { return true; } obj1Proto = obj1Proto.__proto__; } }
const zhangsan = new Student("张三",18,"男"); console.log(myInstance(zhangsan, Student)); console.log(myInstance(zhangsan, Person)); console.log(myInstance(zhangsan, Object)); console.log(myInstance(zhangsan, Array));
|