Advertisement
gheja

Closure Compiler and "extends"

Aug 28th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ClassA
  2. {
  3.   this.alpha = 3;
  4. }
  5.  
  6. class ClassB extends ClassA
  7. {
  8.   this.beta = 42;
  9. }
  10.  
  11.  
  12. // === after running Closure Compiler becomes
  13.  
  14. function ClassA()
  15. {
  16.   this.alpha = 3;
  17. }
  18.  
  19. function ClassB()
  20. {
  21.   ClassA.call(this);
  22.   this.beta = 42;
  23. }
  24.  
  25. q(ClassB, ClassA); // <<<<<<<<
  26.  
  27.  
  28. // === original definition of q() - basically polyfills Object.create and Object.setPrototypeOf
  29. var e = 'function' == typeof Object.create ? Object.create : function(b) {
  30.   function a() {
  31.   }
  32.   a.prototype = b;
  33.   return new a;
  34. }, f;
  35. if ('function' == typeof Object.setPrototypeOf) {
  36.   f = Object.setPrototypeOf;
  37. } else {
  38.   var h;
  39.   a: {
  40.     var k = {s:!0}, m = {};
  41.     try {
  42.       m.__proto__ = k;
  43.       h = m.s;
  44.       break a;
  45.     } catch (b) {
  46.     }
  47.     h = !1;
  48.   }
  49.   f = h ? function(b, a) {
  50.     b.__proto__ = a;
  51.     if (b.__proto__ !== a) {
  52.       throw new TypeError(b + ' is not extensible');
  53.     }
  54.     return b;
  55.   } : null;
  56. }
  57. var n = f;
  58. function q(b, a) {
  59.   b.prototype = e(a.prototype);
  60.   b.prototype.constructor = b;
  61.   if (n) {
  62.     n(b, a);
  63.   } else {
  64.     for (var d in a) {
  65.       if ('prototype' != d) {
  66.         if (Object.defineProperties) {
  67.           var c = Object.getOwnPropertyDescriptor(a, d);
  68.           c && Object.defineProperty(b, d, c);
  69.         } else {
  70.           b[d] = a[d];
  71.         }
  72.       }
  73.     }
  74.   }
  75.   b.F = a.prototype;
  76. }
  77.  
  78.  
  79. // === simplified
  80. var e = Object.create, f = Object.setPrototypeOf, n = f;
  81. function q(b, a) {
  82.   b.prototype = e(a.prototype);
  83.   b.prototype.constructor = b;
  84.   if (n) {
  85.     n(b, a);
  86.   } else {
  87.     for (var d in a) {
  88.       if ('prototype' != d) {
  89.         if (Object.defineProperties) {
  90.           var c = Object.getOwnPropertyDescriptor(a, d);
  91.           c && Object.defineProperty(b, d, c);
  92.         } else {
  93.           b[d] = a[d];
  94.         }
  95.       }
  96.     }
  97.   }
  98.   b.F = a.prototype;
  99. }
  100.  
  101.  
  102. // === simplified again
  103. var e = Object.create, f = Object.setPrototypeOf, n = f;
  104. function q(b, a) {
  105.   b.prototype = e(a.prototype);
  106.   b.prototype.constructor = b;
  107.   n(b, a);
  108.   b.F = a.prototype;
  109. }
  110.  
  111.  
  112. // === and again
  113. function q(b, a) {
  114.   b.prototype = Object.create(a.prototype);
  115.   b.prototype.constructor = b;
  116.   Object.setPrototypeOf(b, a);
  117.   b.F = a.prototype;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement