SwVitaliy

Untitled

May 15th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. count = 0;
  2. function Klacc(){
  3. }
  4. Klacc.extend = function(Child, Parent)
  5.     {
  6.         if (typeof Parent === 'undefined')
  7.             Parent = Klacc;
  8.         var F = function() { };
  9.         F.prototype = Parent.prototype;
  10.         Child.prototype = new F();
  11.         Child.prototype.constructor = Child;
  12.         Child.super = Parent.prototype;
  13.     };
  14. Klacc.prototype.as = function(cl)
  15. {
  16. if ( (typeof this._as === "object") && this._as[cl] )
  17. {
  18.   return this._as[cl];
  19. }
  20. var curr = this;
  21. while ( curr.constructor.name !== cl ) curr = curr.constructor.super;
  22.  
  23. if ( typeof this._as === "undefined" )
  24. {
  25.   this._as = {};
  26. }
  27. else if ( typeof this._as !== "object")
  28. {
  29.   throw { context:this, message:"object already has reserved property \"_as\" invalid type" }
  30. }
  31. this._as[cl] = curr;
  32. return curr;
  33. }
  34. Klacc.prototype.parentOf = function(cl)
  35. {
  36.   var curr = this.as(cl);
  37.   return curr.constructor.super;
  38. }
  39.  
  40. function A() {  }
  41. Klacc.extend(A)
  42. A.prototype.init = function(){ /* A.prototype.init */
  43.   if (++count < 100)
  44.     console.log('A.prototype.init.apply', this);
  45.   this.name = 'A';
  46. }
  47.  
  48.  
  49. function B(){  }
  50. Klacc.extend(B,A)
  51. B.prototype.init = function(){ /* B.prototype.init; this - instance of D */
  52.   if (++count < 100)
  53.     console.log('B.prototype.init.apply', this);
  54. console.log('Parent Of B', this.parentOf('B'));
  55. this.parentOf('B').init.apply(this,arguments);
  56.  
  57.   this.extra = 'im B';
  58. }
  59.  
  60. function C() {  }
  61. Klacc.extend(C,B)
  62. C.prototype.init = function(){ /* C.prototype.init; this - instance of D */
  63.   if (++count < 100)
  64.     console.log('C.prototype.init.apply', this);
  65.  
  66. console.log('Parent Of C', this.parentOf('C'))
  67.     this.parentOf('C').init.apply(this,arguments);
  68.  
  69.   this.extraC = 'mimimi';
  70. }
  71. c= new C
  72.  
  73. function D() {  }
  74.  
  75. Klacc.extend(D,C)
  76.  
  77. D.prototype.init = function(){ /* D.prototype.init */
  78.   if (++count < 100)
  79.     console.log('D.prototype.init.apply', this);
  80. console.log('Parent Of D', this.parentOf('D'))
  81.     this.parentOf('D').init.apply(this,arguments);
  82.  
  83. }
  84.  
  85. d = new D
  86.  
  87. d.init()
Advertisement
Add Comment
Please, Sign In to add comment