13.组合继承.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>组合继承</title>
  8. </head>
  9. <body>
  10. <!--
  11. 方式三:原型链+借用构造函数的组合继承
  12. 1、利用原型链实现对父类型对象的方法继承
  13. 2、利用supper()借用父类型构建函数初始化相同属性
  14. -->
  15. <script>
  16. function Person(name, age) {
  17. this.name = name
  18. this.age = age
  19. }
  20. Person.prototype.setName = function(name){
  21. this.name = name
  22. }
  23. function Student(name, age, price) {
  24. Person.call(this, name, age) //为了得到属性
  25. this.price = price
  26. }
  27. Student.prototype = new Person() //为了能看到父类型的方法
  28. Student.prototype.constructor = Student //修正constructor属性
  29. Student.prototype.setPrice = function(price){
  30. this.price = price
  31. }
  32. var s = new Student('张三',22,800)
  33. s.setName('李四')
  34. s.setPrice(900)
  35. console.log(s.name,s.age,s.price)
  36. </script>
  37. </body>
  38. </html>