美文网首页
76-isPrototypeOf属性

76-isPrototypeOf属性

作者: 仰望_IT | 来源:发表于2019-04-30 17:45 被阅读0次
    • 什么是 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
        

    相关文章

      网友评论

          本文标题:76-isPrototypeOf属性

          本文链接:https://www.haomeiwen.com/subject/fsovnqtx.html