3.继承.js 848 B

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. // 立即执行函数
  3. // function fn1() {}
  4. // fn1()
  5. // (function () {})()
  6. (function () {
  7. // 父级
  8. class All {
  9. constructor(name, age) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. say() {
  14. console.log("你好");
  15. }
  16. }
  17. /**
  18. * 继承:
  19. * 因为想让多个子类同时拥有父类的属性及方法 所以采用继承
  20. * 继承后 子类就会拥有父类相同的内容
  21. * 子类的方法若于父类的方法相同 则会覆盖
  22. * 若想添加新的方法 直接在子类中添加即可
  23. */
  24. class A extends All {
  25. say() {
  26. console.log("哈哈");
  27. }
  28. back() {
  29. console.log("撤回");
  30. }
  31. }
  32. let aa = new A('图图', 3);
  33. console.log(aa);
  34. aa.say();
  35. aa.back();
  36. })();