3.组合继承

时间:2019-10-22
本文章向大家介绍3.组合继承,主要包括3.组合继承使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<body>
<!--
  方式2:借用构造函数继承(假的)
  1.套路:
    定义父类型构造函数
    定义子类型构造函数
    在子类型的构造函数中调用父类型构造
  2.关键
    1.在子类型构造函数中通过call()调用父类型构造函数

-->
  <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)
    //!!!!call是为了继承属性
    //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('Tom',20,4000)
  s.setName('Bob')
  s.setPrice(15000)
  console.log(s.name,s.age,s.price) //假的  //简化代码而已

  </script>
  
</body>

原文地址:https://www.cnblogs.com/lucy-xyy/p/11720597.html