27.继承5.0.html 789 B

123456789101112131415161718192021222324252627282930313233343536
  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. let father = {
  15. name: "LiLi",
  16. age: 18,
  17. getColor: function () {
  18. return this.age;
  19. },
  20. };
  21. function Way(methods) {
  22. let obj = Object.create(methods);
  23. obj.getName = function() {
  24. return this.name;
  25. }
  26. return obj;
  27. }
  28. let child = Way(father);
  29. console.log(child.getColor());
  30. console.log(child.getName());
  31. </script>
  32. </body>
  33. </html>