Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // 28: class - super in constructor
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('Inside a class`s constructor `super()` can be used', () => {
  6. it('`extend` a class and use `super()` to call the parent constructor', () => {
  7. class A {constructor() { this.levels = 1; }}
  8. class B extends A{
  9. constructor() {
  10. super()
  11. this.levels++;
  12. }
  13. }
  14. assert.equal(new B().levels, 2);
  15. });
  16. it('`super()` may also take params', () => {
  17. class A {constructor(startValue=1, addTo=1) { this.counter = startValue + addTo; }}
  18. class B extends A {
  19. constructor(...args) {
  20. super(...args);
  21. this.counter++;
  22. }
  23. }
  24. assert.equal(new B(42, 2).counter, 45);
  25. });
  26. it('it is important where you place your `super()` call!', () => {
  27. class A {inc() { this.countUp = 1; }}
  28. class B extends A {
  29. inc() {
  30.  
  31. this.countUp = 2;
  32. super.inc();
  33. return this.countUp;
  34. }
  35. }
  36. assert.equal(new B().inc(), 1);
  37. });
  38. it('use `super.constructor` to find out if there is a parent constructor', () => {
  39. class ParentClassA {constructor() {"parent"}}
  40. class B extends ParentClassA {
  41. constructor() {
  42. super();
  43. this.isTop = '' + super.constructor;
  44. }
  45. }
  46. assert(new B().isTop.includes('ParentClassA'), new B().isTop);
  47. });
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement