/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//static object (it's like Math) var StaticObject = function() { //*** Private members ***// var privateVar = "Private"; function privateFunc(){ return "Private"; }; //*** Public members ***// return { publicVar: "Public", getPrivateVar: function(){ return privateVar; }, setPrivateVar: function(v){ privateVar = v; } //Could use getters & setters instead, but IE doesn't support them. Example: /*get privateVar(){ return privateVar; }, set privateVar(v){ privateVar = v; }*/ }; }(); //base class; inherits Object function BaseClass(arg1, arg2) { //*** Object inheritance setup ***// //Set up protected member access for derived classes //Protected members aren't really supported in JavaScript, but here's a way to implement them. var protected = {}; this.setupProtected = function() { if(!(this.setupProtected.caller.prototype instanceof BaseClass)) throw (new ReferenceError("setupProtected is not defined")); var p = {}; p.get = function(str){ return protected[str]; }; p.set = function(str, val){ return (protected[str] = val); }; return p; }; //*** Private members; only accessible within this class ***// var privateVar = "Private"; function privateFunc(){ return privateVar; }; //*** Protected members; only accessible within this class and derived classes ***// protected.protectedVar = "Protected"; protected.protectedFunc = function(){ return protected.protectedVar; }; //*** Public members ***// this.toString = function(){ return "[object BaseClass]"; }; //override inherited toString() method this.publicVar = "Public"; //Could use getters & setters, but IE doesn't support them. Example: /*this.__defineGetter__("publicVar", function(){ return "Public"; }); this.__defineSetter__("publicVar", function(v){ publicVar = v; });*/ this.publicFunc = function(){ return publicVar; }; } //*** static methods/properties ***/ //these are not inherited (it's like String.fromCharCode()) BaseClass.staticMethod = function(){ return "Static"; }; //subclass; inherits BaseClass function SubClass(arg1, arg2) { //*** Object inheritance setup ***// BaseClass.apply(this, [arg1, arg2]); //construct this object using the BaseClass constructor this.constructor = SubClass; //reference this function as the object's constructor var baseProtected = this.setupProtected(); //Note: do not call this within a member function or it won't work //Protected members of BaseClass can now be accessed using, e.g.: // baseProtected.get("protectedVar") // baseProtected.set("protectedVar", 42) //Set up protected member access for derived classes //Protected members aren't really supported in JavaScript, but here's a way to implement them. //This implementation is different from that in BaseClass because if the protected member is // not defined in SubClass it continues up the chain to look in BaseClass for it. var protected = {}; this.setupProtected = function() { if(!(this.setupProtected.caller.prototype instanceof SubClass)) throw (new ReferenceError("setupProtected is not defined")); var p = {}; p.get = function(str) { var v; if(typeof protected[str] != "undefined") v = protected[str]; else v = baseProtected.get(str); //try accessing inherited member return v; }; p.set = function(str, val) { var v; if(typeof protected[str] != "undefined") v = protected[str] = val; else v = baseProtected.set(str, val); //try setting inherited member return v; }; return p; }; //*** Private members; only accessible within this class ***// //... //*** Protected members; only accessible within this class and derived classes ***// //... //*** Public members ***// this.toString = function(){ return "[object SubClass]"; }; //override inherited toString() method //... } SubClass.prototype = new BaseClass(); //put SubClass into the prototype chain as a subclass of BaseClass //*** static methods/properties ***/ //...