4.super.js 712 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. // (function(){})()
  3. (function () {
  4. class Money {
  5. constructor(name, num) {
  6. this.name = name;
  7. this.num = num;
  8. }
  9. say() {
  10. console.log("你好");
  11. }
  12. }
  13. /**
  14. * super
  15. * 如果子类要使用父类中属性
  16. * 子类的构造函数必须对父类的构造函数重新进行接受
  17. * 使用super
  18. */
  19. class A extends Money {
  20. constructor(name, num, age) {
  21. super(name, num);
  22. this.age = age;
  23. }
  24. }
  25. class B extends Money {
  26. }
  27. let a = new A('喜羊羊', 100, 18);
  28. let b = new B('灰太狼', 50);
  29. console.log(a, 'a');
  30. console.log(b, 'b');
  31. })();