JavaScript-isPrototypeOf属性

152 阅读1分钟

1.什么是isPrototypeOf属性

isPrototypeOf用于判断 一个对象是否是另一个对象的原型

        class Person{
            name = "lnj";
        }
        let  p = new Person();
        // Person.prototype 是否是实例对象P的原型
        console.log(Person.prototype.isPrototypeOf(p)); // true
        

2.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();
        console.log(Person.prototype.isPrototypeOf(stu)); // true

在这里插入图片描述


JS面向对象 prototype

学习笔记❥(^_-)