13.组合继承.html 958 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <!--
  10. 组合继承:
  11. 原型链继承 + 构造函数继承
  12. 实现:call/apply + 原型链
  13. -->
  14. <script>
  15. function Person(x) {
  16. this.x = x;
  17. this.name = '图图';
  18. this.age = 3;
  19. this.list = ['吃饭', '睡觉', '打豆豆'];
  20. }
  21. Person.prototype.say = function () {
  22. console.log("你好");
  23. }
  24. function Child(val) {
  25. Person.call(this,val)
  26. }
  27. Child.prototype = new Person();
  28. let c1 = new Child('北京');
  29. let c2 = new Child('哈尔滨');
  30. c2.list.push("12");
  31. console.log(c1, 'c1', c1.list)
  32. console.log(c2, 'c2', c2.list)
  33. c1.say()
  34. </script>
  35. </body>
  36. </html>