3.组合继承

时间:2019-10-22 16:36:16   收藏:0   阅读:63
<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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!