23.继承1.0.html 914 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. /**
  11. * 1.原型链继承:
  12. * 构造函数 原型 实例
  13. */
  14. function Father() {
  15. this.name = "Lucy";
  16. this.arr = [1, 2, 3];
  17. }
  18. function Child() {
  19. this.age = 18;
  20. }
  21. Child.prototype = new Father();
  22. let newChild = new Child();
  23. console.log(newChild.arr,'新的')
  24. let a1 = new Child();
  25. let a2 = new Child();
  26. /**
  27. * 因为两个实例使用的是同一个原型,所以一个修改另一个也会发生改变
  28. */
  29. a2.arr.push(10);
  30. a1.name += 10; //a1.name = a1.name + 10
  31. a2.name += 20;
  32. console.log(a1,'a1');
  33. console.log(a2,'a2');
  34. </script>
  35. </body>
  36. </html>