Advertisement
nekto-unnamed

Untitled

Oct 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let Parent = function Parent() {
  2.     console.log('Parent constructor');
  3. };
  4.  
  5. Parent.prototype = {
  6.     constructor: Parent,
  7.  
  8.     foo: function () {
  9.         console.log('Parent foo');
  10.     },
  11.  
  12.     super: function() {
  13.         let caller = this.super.caller;
  14.         var parent = this.superclass;
  15.         // Вызов конструктора
  16.         if (this instanceof this.super.caller) {
  17.             parent.apply(this);
  18.         } else {
  19.             if (caller.name && parent.prototype[caller.name]) {
  20.                 parent.prototype[caller.name].apply(this);
  21.             }
  22.         }
  23.     }
  24. };
  25.  
  26. Parent.inherit = function (Constr, Prototype) {
  27.     Constr.prototype = Object.create(this.prototype);
  28.     Object.assign(Constr.prototype, Prototype);
  29.     Constr.prototype.constructor = Constr;
  30.     Constr.inherit = this.inherit;
  31.     Constr.prototype.superclass = this;
  32.     return Constr;
  33. };
  34.  
  35.  
  36. let Child = Parent.inherit(function Child() {
  37.     this.super();
  38. }, {
  39.     foo: function() {
  40.         this.super();
  41.     }
  42. });
  43.  
  44.  
  45. (new Child).foo();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement