首页 > Web开发 > 详细

js的组合继承

时间:2018-09-20 10:42:51      阅读:163      评论:0      收藏:0      [点我收藏+]
 <script>
          //组合继承:原型链继承+借用构造函数
          function Person(name,age){
                this.name=name;
                this.age=age;
          }

          Person.prototype.setName=function (name){
              this.name=name;
          }

          function Student(name,age,price){
              Person.call(this,name,age);         //相当于调用this.Person(name,age),等价于this.name=name;this.age=age;
              this.price=price;

          }
            
            Student.prototype=new Person();
           Student.prototype.constructor=Student;
           Student.prototype.setPrice=function (price){
                   this.price=price;
           }


           var s=new Student(‘zain‘,26,20000);
           console.log(s.name,s.age,s.price);
    </script>

  

js的组合继承

原文:https://www.cnblogs.com/zhumeiming/p/9678664.html

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