69-继承方式三

  • 将Student构造函数的原型对象改为Person构造函数的原型对象
    • 注意点: 要想使用Person原型对象中的属性和方法, 那么就必须将Student的原型对象改为Person的原型对象才可以

          function Person(myName, myAge) {
              // let per = new Object();
              // let this = per;
              // this = stu;
              this.name = myName; // stu.name = myName;
              this.age = myAge;   // stu.age = myAge;
      
              // this.say = function () {    // stu.say = function () {}
              //     console.log(this.name, this.age);
              // }
              // return this;
          }
      
          // 如果将say方法放到Person原型对象中, 那么用继承方式二将不能访问say, 这就是继承方式二的弊端
          Person.prototype.say = function () {
              console.log(this.name, this.age);
          }
      
          function Student(myName, myAge, myScore) {
              // let stu = new Object();
              // let this = stu;
              Person.call(this, myName, myAge); // Person.call(stu);
              this.score = myScore;
              this.study = function () {
                  console.log("day day up");
              }
              // return this;
          }
      
          // 解决继承方式二的弊端, 也就是继承方式三
          // 将Student构造函数的原型对象改为Person构造函数的原型对象
          // 注意点: 要想使用Person原型对象中的属性和方法, 那么就必须将Student的原型对象改为Person的原型对象才可以
          Student.prototype = Person.prototype;
          Student.prototype.constructor = Student;
      
          let stu = new Student("ww", 19, 99);
          console.log(stu.score);
          stu.say();
          stu.study();
      




  • 继承方式三的弊端
    • 由于修改了Person原型对象的constructor属性, 所以破坏了Person的三角恋关系

    • 由于PersonStudent的原型对象是同一个, 所以给Student的元素添加方法的时候, Person也会新增方法

         function Person(myName, myAge) {
              // let per = new Object();
              // let this = per;
              // this = stu;
              this.name = myName; // stu.name = myName;
              this.age = myAge;   // stu.age = myAge;
      
              // this.say = function () {    // stu.say = function () {}
              //     console.log(this.name, this.age);
              // }
              // return this;
          }
      
          Person.prototype.say = function () {
              console.log(this.name, this.age);
          }
      
          function Student(myName, myAge, myScore) {
              // let stu = new Object();
              // let this = stu;
              Person.call(this, myName, myAge); // Person.call(stu);
              this.score = myScore;
              this.study = function () {
                  console.log("day day up");
              }
              // return this;
          }
      
          /*
          弊端:
          1.由于修改了Person原型对象的constructor属性, 所以破坏了Person的三角恋关系
          2.由于Person和Student的原型对象是同一个, 所以给Student的元素添加方法的时候, Person也会新增方法
          */
          Student.prototype = Person.prototype;
          Student.prototype.constructor = Student;
          Student.prototype.run = function(){
              console.log("run");
          }
      
          let per = new Person();
          per.run();  // run
      
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。