首页 > 其他 > 详细

3.组合继承

时间:2019-10-22 16:36:16      阅读:63      评论:0      收藏:0      [点我收藏+]
<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>

 

3.组合继承

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

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