28.继承.6.0.html 950 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. <script>
  10. /**
  11. * 寄生组合式继承
  12. *
  13. */
  14. function Father() {
  15. this.name = "小明";
  16. this.arr = [1, 2, 3];
  17. }
  18. Father.prototype.getName = function () {
  19. return this.name;
  20. };
  21. function Child() {
  22. Father.call(this);
  23. this.color = "red";
  24. }
  25. function Way(father,child) {
  26. child.prototype = Object.create(father.prototype);
  27. child.prototype.constructor = child;
  28. }
  29. Way(Father,Child);
  30. Child.prototype.getColor = function() {
  31. return this.color;
  32. }
  33. let a1 = new Child();
  34. console.log(a1.getName(),'a1.getName');
  35. console.log(a1.getColor(),'a1.getColor');
  36. </script>
  37. </body>
  38. </html>