Advertisement
cirossmonteiro

Pseudo-multiple inheritance

Apr 17th, 2024 (edited)
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.60 KB | Source Code | 0 0
  1. const superclass = (...list) =>
  2.     list.reduce((ac, cls) => cls(ac), class {});
  3.  
  4. const A = X => class extends (X || class {}) {
  5.     foo() {
  6.     console.log(`fooA`);
  7.   }
  8. }
  9.  
  10. const B = X => class extends (X || class {}) {
  11.     foo() {
  12.     super.foo();
  13.     console.log(`fooB`);
  14.   }
  15. }
  16.  
  17. const C = X => class extends (X || class {}) {
  18.     foo() {
  19.     super.foo();
  20.     console.log(`fooC`);
  21.   }
  22. }
  23.  
  24. class D extends superclass (A, B, C) {
  25.   foo() {
  26.     super.foo();
  27.     console.log(`fooD`);
  28.   }
  29. }
  30.  
  31. const d = new D();
  32. d.foo()
  33.  
  34. // Result: fooA\nfooB\nfooC\fooD
  35. // based on https://stackoverflow.com/a/35925061
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement