13.组合继承.html 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. Child.prototype.constructor = Child;
  29. let c1 = new Child('北京');
  30. let c2 = new Child('哈尔滨');
  31. c2.list.push("12");
  32. console.log(c1.constructor,'打印');
  33. console.log(c1, 'c1', c1.list)
  34. console.log(c2, 'c2', c2.list)
  35. c1.say()
  36. </script>
  37. </body>
  38. </html>