JavaScript Object Inheritance


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //static object (it's like Math)
  2. var StaticObject = function()
  3. {
  4. //*** Private members ***//
  5.  
  6. var privateVar = "Private";
  7. function privateFunc(){ return "Private"; };
  8.  
  9. //*** Public members ***//
  10.  
  11. return {
  12. publicVar: "Public",
  13. getPrivateVar: function(){ return privateVar; },
  14. setPrivateVar: function(v){ privateVar = v; }
  15. //Could use getters & setters instead, but IE doesn't support them. Example:
  16. /*get privateVar(){ return privateVar; },
  17. set privateVar(v){ privateVar = v; }*/
  18. };
  19. }();
  20.  
  21.  
  22. //base class; inherits Object
  23. function BaseClass(arg1, arg2)
  24. {
  25. //*** Object inheritance setup ***//
  26.  
  27. //Set up protected member access for derived classes
  28. //Protected members aren't really supported in JavaScript, but here's a way to implement them.
  29. var protected = {};
  30. this.setupProtected = function()
  31. {
  32. if(!(this.setupProtected.caller.prototype instanceof BaseClass))
  33. throw (new ReferenceError("setupProtected is not defined"));
  34. var p = {};
  35. p.get = function(str){ return protected[str]; };
  36. p.set = function(str, val){ return (protected[str] = val); };
  37. return p;
  38. };
  39.  
  40. //*** Private members; only accessible within this class ***//
  41.  
  42. var privateVar = "Private";
  43. function privateFunc(){ return privateVar; };
  44.  
  45. //*** Protected members; only accessible within this class and derived classes ***//
  46.  
  47. protected.protectedVar = "Protected";
  48. protected.protectedFunc = function(){ return protected.protectedVar; };
  49.  
  50. //*** Public members ***//
  51.  
  52. this.toString = function(){ return "[object BaseClass]"; }; //override inherited toString() method
  53.  
  54. this.publicVar = "Public";
  55. //Could use getters & setters, but IE doesn't support them. Example:
  56. /*this.__defineGetter__("publicVar", function(){ return "Public"; });
  57. this.__defineSetter__("publicVar", function(v){ publicVar = v; });*/
  58. this.publicFunc = function(){ return publicVar; };
  59. }
  60. //*** static methods/properties ***/
  61. //these are not inherited (it's like String.fromCharCode())
  62. BaseClass.staticMethod = function(){ return "Static"; };
  63.  
  64.  
  65. //subclass; inherits BaseClass
  66. function SubClass(arg1, arg2)
  67. {
  68. //*** Object inheritance setup ***//
  69.  
  70. BaseClass.apply(this, [arg1, arg2]); //construct this object using the BaseClass constructor
  71. this.constructor = SubClass; //reference this function as the object's constructor
  72.  
  73. var baseProtected = this.setupProtected(); //Note: do not call this within a member function or it won't work
  74. //Protected members of BaseClass can now be accessed using, e.g.:
  75. // baseProtected.get("protectedVar")
  76. // baseProtected.set("protectedVar", 42)
  77.  
  78. //Set up protected member access for derived classes
  79. //Protected members aren't really supported in JavaScript, but here's a way to implement them.
  80. //This implementation is different from that in BaseClass because if the protected member is
  81. // not defined in SubClass it continues up the chain to look in BaseClass for it.
  82. var protected = {};
  83. this.setupProtected = function()
  84. {
  85. if(!(this.setupProtected.caller.prototype instanceof SubClass))
  86. throw (new ReferenceError("setupProtected is not defined"));
  87. var p = {};
  88. p.get = function(str)
  89. {
  90. var v;
  91. if(typeof protected[str] != "undefined") v = protected[str];
  92. else v = baseProtected.get(str); //try accessing inherited member
  93. return v;
  94. };
  95. p.set = function(str, val)
  96. {
  97. var v;
  98. if(typeof protected[str] != "undefined") v = protected[str] = val;
  99. else v = baseProtected.set(str, val); //try setting inherited member
  100. return v;
  101. };
  102. return p;
  103. };
  104.  
  105. //*** Private members; only accessible within this class ***//
  106.  
  107. //...
  108.  
  109. //*** Protected members; only accessible within this class and derived classes ***//
  110.  
  111. //...
  112.  
  113. //*** Public members ***//
  114.  
  115. this.toString = function(){ return "[object SubClass]"; }; //override inherited toString() method
  116.  
  117. //...
  118. }
  119. SubClass.prototype = new BaseClass(); //put SubClass into the prototype chain as a subclass of BaseClass
  120. //*** static methods/properties ***/
  121. //...

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.