16.寄生组合式继承.html 983 B

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. <script>
  10. function Father(a) {
  11. this.name = '图图';
  12. this.list = ['吃饭', '睡觉', '打豆豆'];
  13. this.address = a;
  14. }
  15. Father.prototype.say = function () {
  16. console.log("你好");
  17. }
  18. function Child(val) {
  19. Father.call(this,val);
  20. }
  21. // Child.prototype = new Father();
  22. function createObj(c, f) {
  23. c.prototype = Object.create(f.prototype);
  24. c.prototype.constructor = c;
  25. }
  26. createObj(Child, Father)
  27. let c1 = new Child('北京');
  28. let c2 = new Child('哈尔滨');
  29. c1.list.push("hahaha")
  30. console.log(c1, 'c1');
  31. console.log(c2, 'c2');
  32. c1.say();
  33. c2.say();
  34. </script>
  35. </body>
  36. </html>