首页 > 编程语言 > 详细

javaScript-继承方式

时间:2021-08-17 10:21:09      阅读:22      评论:0      收藏:0      [点我收藏+]

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;

 

javaScript-继承方式

原文:https://www.cnblogs.com/heartY/p/15150590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!