javaScript-继承方式

1.js中继承的终极方法
1.1在子类的构造函数中通过call借助父类的构造函数
1.2将子类的原型对象修改为父类的实例对象

    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;
      // return this;
    }
    Person.prototype.say = function () {
      console.log(this.name, this.age, this.score);
    }
    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.prototype = new Person();
    Person.prototype.constructor = Student;

 

上一篇:Python类的self


下一篇:让你彻底理解Typescript中静态成员和抽象方法