Advertisement
Guest User

Untitled

a guest
Jul 30th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ====================================================
  3. // * Base Class
  4. // ====================================================
  5.  
  6. class Base {
  7.     test_return() {
  8.         return 0;
  9.     }
  10.  
  11.     test_super() {
  12.         console.log("Base");
  13.     }
  14.  
  15.     test_this() {
  16.         if(!this._val) this._val = 0;
  17.         return ++this._val;
  18.     }
  19. }
  20.  
  21. // ====================================================
  22. // * Child Class
  23. // ====================================================
  24.  
  25. class Child extends Base {
  26.     test_return() {
  27.         return super.test_return() * 2;
  28.     }
  29.  
  30.     test_super() {
  31.         super.test_super();
  32.         console.log("Child");
  33.     }
  34.  
  35.     test_this() {
  36.         super.test_this();
  37.         return ++this._val;
  38.     }
  39. }
  40.  
  41. /*** ---------------------------------------------------------------------------- ***/
  42.  
  43. // ====================================================
  44. // * Namespace (to store old functions)
  45. // ====================================================
  46.  
  47. var Dev = Dev || {};
  48. Dev.PluginName = Dev.PluginName || {};
  49.  
  50. (function($) { // $ = Dev.PluginName
  51.  
  52. // ====================================================
  53. // * Create Merge Class
  54. // ====================================================
  55.  
  56. function MergeClass(classObj, es6Class, namespace) {
  57.     // Get Class Name as String (for ex: "Base")
  58.     const className = classObj.prototype.constructor.name;
  59.  
  60.     // Add Class Name Property to Namespace
  61.     namespace[className] = {};
  62.  
  63.     // Loop Through ES6 Prototype
  64.     Object.getOwnPropertyNames(es6Class.prototype).forEach(function(name) {
  65.  
  66.         // Ignore Constructor
  67.         if(name === "constructor") return;
  68.  
  69.         // If the Function Already Exists, Copy Into Namespace
  70.         if(classObj.prototype[name]) {
  71.             namespace[className][name] = classObj.prototype[name];
  72.         }
  73.  
  74.         // Add/Overwrite Function Into Original Class
  75.         classObj.prototype[name] = es6Class.prototype[name];
  76.     });
  77. }
  78.  
  79. // ====================================================
  80. // * Modify Base Class
  81. // ====================================================
  82.  
  83. class ModifiedBase {
  84.     test_return() {
  85.         return 12;
  86.     }
  87.  
  88.     test_super() {
  89.         $.Base.test_super.call(this);
  90.         console.log("Base Modified");
  91.     }
  92.  
  93.     test_this() {
  94.         $.Base.test_this.call(this);
  95.         return ++this._val;
  96.     }
  97. }
  98.  
  99. // Add Content from ModifiedBase to Base
  100. MergeClass(Base, ModifiedBase, $);
  101.  
  102. // ====================================================
  103. // * Test Child Class
  104. // ====================================================
  105.  
  106. const c = new Child();
  107.  
  108. // normally, this would be 0 since the Base.test_return is 0
  109. // but since Base now returns 12, this prints 24
  110. // base behavior modified while still retaining child behavior
  111. console.log(c.test_return()); // prints 24
  112.  
  113. // this successfully prints the following (in order):
  114. // Base
  115. // Base Modified
  116. // Child
  117. c.test_super();
  118.  
  119. // normally, this would return 2, since it is incremented once in both Base and Child
  120. // but now it increments by 3, since an increment is included in modified Base:
  121. console.log(c.test_this()) // prints 3
  122. console.log(c.test_this()) // prints 6
  123.  
  124. })(Dev.PluginName);
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement