Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. // Class for creating multi inheritance.
  2. class multi {
  3. // Inherit method to create base classes.
  4. static inherit(..._bases) {
  5. class classes {
  6.  
  7. // The base classes
  8. get base() {
  9. return _bases;
  10. }
  11.  
  12. constructor(..._args) {
  13. var index = 0;
  14.  
  15. for (let b of this.base) {
  16. let obj = new b(_args[index++]);
  17. multi.copy(this, obj);
  18. }
  19. }
  20.  
  21. }
  22.  
  23. // Copy over properties and methods
  24. for (let base of _bases) {
  25. multi.copy(classes, base);
  26. multi.copy(classes.prototype, base.prototype);
  27. }
  28.  
  29. return classes;
  30. }
  31.  
  32. // Copies the properties from one class to another
  33. static copy(_target, _source) {
  34. for (let key of Reflect.ownKeys(_source)) {
  35. if (key !== "constructor" && key !== "prototype" && key !== "name") {
  36. let desc = Object.getOwnPropertyDescriptor(_source, key);
  37. Object.defineProperty(_target, key, desc);
  38. }
  39. }
  40. }
  41. }
  42.  
  43. module.exports = multi;
  44.  
  45. /**********/
  46. /* Sample */
  47. /**********/
  48.  
  49. // Single Inherit
  50. class person extends ages
  51.  
  52. // Multi Inherit
  53. class person extends multi.inherit(ages, genders)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement