class-inheritance.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. test("method inheritance", () => {
  2. class Parent {
  3. method() {
  4. return 3;
  5. }
  6. }
  7. class Child extends Parent {}
  8. const p = new Parent();
  9. const c = new Child();
  10. expect(p.method()).toBe(3);
  11. expect(c.method()).toBe(3);
  12. });
  13. test("method overriding", () => {
  14. class Parent {
  15. method() {
  16. return 3;
  17. }
  18. }
  19. class Child extends Parent {
  20. method() {
  21. return 10;
  22. }
  23. }
  24. const p = new Parent();
  25. const c = new Child();
  26. expect(p.method()).toBe(3);
  27. expect(c.method()).toBe(10);
  28. });
  29. test("parent method reference with super", () => {
  30. class Parent {
  31. method() {
  32. return 3;
  33. }
  34. }
  35. class Child extends Parent {
  36. method() {
  37. return super.method() * 2;
  38. }
  39. }
  40. const p = new Parent();
  41. const c = new Child();
  42. expect(p.method()).toBe(3);
  43. expect(c.method()).toBe(6);
  44. });
  45. test("child class access to parent class initialized properties", () => {
  46. class Parent {
  47. constructor() {
  48. this.x = 3;
  49. }
  50. }
  51. class Child extends Parent {}
  52. const p = new Parent();
  53. const c = new Child();
  54. expect(p.x).toBe(3);
  55. expect(c.x).toBe(3);
  56. });
  57. test("child class modification of parent class properties", () => {
  58. class Parent {
  59. constructor() {
  60. this.x = 3;
  61. }
  62. }
  63. class Child extends Parent {
  64. change() {
  65. this.x = 10;
  66. }
  67. }
  68. const p = new Parent();
  69. const c = new Child();
  70. expect(p.x).toBe(3);
  71. expect(c.x).toBe(3);
  72. c.change();
  73. expect(c.x).toBe(10);
  74. });
  75. test("inheritance and hasOwnProperty", () => {
  76. class Parent {
  77. constructor() {
  78. this.x = 3;
  79. }
  80. }
  81. class Child extends Parent {
  82. method() {
  83. this.y = 10;
  84. }
  85. }
  86. const p = new Parent();
  87. const c = new Child();
  88. expect(p.hasOwnProperty("x")).toBeTrue();
  89. expect(p.hasOwnProperty("y")).toBeFalse();
  90. expect(c.hasOwnProperty("x")).toBeTrue();
  91. expect(c.hasOwnProperty("y")).toBeFalse();
  92. c.method();
  93. expect(c.hasOwnProperty("x")).toBeTrue();
  94. expect(c.hasOwnProperty("y")).toBeTrue();
  95. });
  96. test("super constructor call from child class with argument", () => {
  97. class Parent {
  98. constructor(x) {
  99. this.x = x;
  100. }
  101. }
  102. class Child extends Parent {
  103. constructor() {
  104. super(10);
  105. }
  106. }
  107. const p = new Parent(3);
  108. const c = new Child(3);
  109. expect(p.x).toBe(3);
  110. expect(c.x).toBe(10);
  111. });
  112. test("advanced 'extends' RHS", () => {
  113. const foo = {
  114. bar() {
  115. return {
  116. baz() {
  117. return function () {
  118. return function () {
  119. return { quux: Number };
  120. };
  121. };
  122. },
  123. };
  124. },
  125. };
  126. class Foo extends foo.bar()["baz"]()`qux`().quux {}
  127. expect(new Foo()).toBeInstanceOf(Number);
  128. });
  129. test("issue #7045, super constructor call from child class in catch {}", () => {
  130. class Parent {
  131. constructor(x) {
  132. this.x = x;
  133. }
  134. }
  135. class Child extends Parent {
  136. constructor() {
  137. try {
  138. throw new Error("Error in Child constructor");
  139. } catch (e) {
  140. super(e.message);
  141. }
  142. }
  143. }
  144. const c = new Child();
  145. expect(c.x).toBe("Error in Child constructor");
  146. });
  147. test("Issue #7044, super property access before super() call", () => {
  148. class Foo {
  149. constructor() {
  150. super.bar;
  151. }
  152. }
  153. new Foo();
  154. });
  155. test("Issue #8574, super property access before super() call", () => {
  156. var hit = false;
  157. class Foo extends Object {
  158. constructor() {
  159. expect(() => {
  160. const foo = super.bar();
  161. }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
  162. hit = true;
  163. }
  164. }
  165. // Note: We catch two exceptions here.
  166. expect(() => {
  167. new Foo();
  168. }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
  169. expect(hit).toBeTrue();
  170. });