js的组合继承

 <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>

  

posted @ 2018-09-20 07:27  Z.ain  阅读(137)  评论(0编辑  收藏  举报