SwVitaliy

Untitled

May 15th, 2012
60
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 && curr.constructor.name !== cl ) curr = curr.constructor.super;
  22. if ( !curr )
  23. {
  24.   throw { context:this, message:"объект не имеет возможности предстаиться классом \"{cl}\" ".replace('{cl}', cl) }
  25. }
  26.  
  27. if ( typeof this._as === "undefined" )
  28. {
  29.   this._as = {};
  30. }
  31. else if ( typeof this._as !== "object")
  32. {
  33.   throw { context:this, message:"object already has reserved property \"_as\" invalid type" }
  34. }
  35. this._as[cl] = curr;
  36. return curr;
  37. }
  38. Klacc.prototype.parentOf = function(cl)
  39. {
  40.   return this.as(cl).constructor.super;
  41. }
  42.  
  43. function A() {  }
  44. Klacc.extend(A)
  45. A.prototype.init = function(){ /* A.prototype.init */
  46.   if (++count < 100)
  47.     console.log('A.prototype.init.apply', this);
  48.   this.name = 'A';
  49. }
  50.  
  51.  
  52. function B(){  }
  53. Klacc.extend(B,A)
  54. B.prototype.init = function(){ /* B.prototype.init; this - instance of D */
  55.   if (++count < 100)
  56.     console.log('B.prototype.init.apply', this);
  57. console.log('Parent Of B', this.parentOf('B'));
  58. this.parentOf('B').init.apply(this,arguments);
  59.  
  60.   this.extra = 'im B';
  61. }
  62.  
  63. function C() {  }
  64. Klacc.extend(C,B)
  65. C.prototype.init = function(){ /* C.prototype.init; this - instance of D */
  66.   if (++count < 100)
  67.     console.log('C.prototype.init.apply', this);
  68.  
  69. console.log('Parent Of C', this.parentOf('C'))
  70.     this.parentOf('C').init.apply(this,arguments);
  71.  
  72.   this.extraC = 'mimimi';
  73. }
  74. c= new C
  75.  
  76. function D() {  }
  77.  
  78. Klacc.extend(D,C)
  79.  
  80. D.prototype.init = function(){ /* D.prototype.init */
  81.   if (++count < 100)
  82.     console.log('D.prototype.init.apply', this);
  83. console.log('Parent Of D', this.parentOf('ddgfdsg'));return;
  84.     this.parentOf('D').init.apply(this,arguments);
  85.  
  86. }
  87.  
  88. d = new D
  89.  
  90. d.init()
Advertisement
Add Comment
Please, Sign In to add comment