您当前的位置: 首页 > 

暂无认证

  • 3浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JS继承4

发布时间:2020-08-18 00:38:48 ,浏览量:3

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> 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); } function Student(myName, myAge, myScore) { Person.call(this, myName, myAge); this.score = myScore; this.study = function () { console.log("day day up"); } } /*
        弊端:
        1.由于修改了Person原型对象的constructor属性, 所以破坏了Person的三角恋关系
        2.由于Person和Student的原型对象是同一个, 所以给Student的元素添加方法, Person也会新增方法
         */ // Student.prototype = Person.prototype; Student.prototype = new Person();// Student.prototype.constructor = Student; Student.prototype.run = function(){ console.log("run"); } let per = new Person(); per.run(); /*
        1.js中继承的终极方法
        1.1在子类的构造函数中通过call借助父类的构造函数
        1.2将子类的原型对象修改为父类的实例对象
        */ // let stu = new Student("ww", 19, 99); // console.log(stu.score); // stu.say(); // stu.study(); </script> </body> </html> 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.0474s