Return to Snippet

Revision: 8098
at January 5, 2009 16:30 by wizard04


Updated Code
//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 ***/
//...

Revision: 8097
at October 1, 2008 15:06 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	//Note that in cases like this where the function doesn't require the closure, it would be more
	// efficient to make it a property of the prototype (or better yet, a static property of the class
	// itself since it doesn't use the |this| keyword either). That *would* make it public, though.
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them.
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
		{
			var p = {};
			p.get = function(str){ return protected[str]; };
			p.set = function(str, val){ return (protected[str] = val); };
			return p;
		}
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return protected.protectedVar; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	this.constructor = SubClass;	//reference this function as the object's constructor
	
	var prot = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., prot.get("protectedVar") or
	// prot.set("protectedVar", 15)
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
		{
			var p = {};
			p.get = function(str)
			{
				var v;
				if(protected[str] !== undefined) v = protected[str];
				else v = prot.get(str);	//try accessing inherited member
				return v;
			};
			p.set = function(str, val)
			{
				var v;
				if(protected[str] !== undefined) v = protected[str] = val;
				else v = prot.set(str, val);	//try setting inherited member
				return v;
			};
			return p;
		}
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8096
at September 22, 2008 15:36 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val;
				try{ val = eval("protected."+str); }catch(e){}
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** Public members ***//
	
	this.constructor = SubClass;	//reference this function as the object's constructor
	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

Revision: 8095
at September 22, 2008 15:29 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	this.constructor = SubClass;			//reference this function as the object's constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val;
				try{ val = eval("protected."+str); }catch(e){}
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8094
at September 22, 2008 15:28 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	this.constructor = SubClass;
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val;
				try{ val = eval("protected."+str); }catch(e){}
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8093
at September 4, 2008 09:48 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val;
				try{ val = eval("protected."+str); }catch(e){}
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8092
at September 4, 2008 09:47 by wizard04


Updated Code
//static object (it's like Math)
var StaticObject = function()
{
	//*** Private members ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val;
				try{ val = eval("protected."+str); }catch(e){}
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

	//*** 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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val = eval("protected."+str);
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8091
at September 4, 2008 09:37 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str)
			{
				var val = eval("protected."+str);
				if(val === undefined) val = getProtected(str);	//try accessing inherited member
				return val;
			};
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	//...
	
	//*** 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

Revision: 8090
at September 4, 2008 09:03 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		throw (new ReferenceError("setupProtected is not defined"));
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();	//Note: do not call this within a function or it won't work
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str){ return eval("protected."+str); };
		return undefined;
	};
	
	//...
	
	//*** 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

Revision: 8089
at September 4, 2008 08:39 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Protected members aren't really supported in JavaScript, but here's a way to implement them
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof BaseClass)
			return function(str){ return eval("protected."+str); };
		return undefined;
	};
	
	protected.protectedVar = "Protected";
	protected.protectedFunc = function(){ return "Protected"; };
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "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; });*/
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	var getProtected = this.setupProtected();
	//Protected members of BaseClass can now be accessed using, e.g., getProtected("protectedVar")
	
	//*** Private members; only accessible within this class ***//
	
	//...
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Set up protected member access for derived classes
	var protected = {};
	this.setupProtected = function()
	{
		if(this.setupProtected.caller.prototype instanceof SubClass)
			return function(str){ return eval("protected."+str); };
		return undefined;
	};
	
	//...
	
	//*** 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

Revision: 8088
at September 2, 2008 16:48 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//Not supported in JavaScript
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "Public"; };
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	//*** Private and Public members specific to SubClass ***//
	
	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

Revision: 8087
at September 2, 2008 14:21 by wizard04


Updated Code
//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)
{
	//*** Private members; only accessible within this class ***//
	
	var privateVar = "Private";
	function privateFunc(){ return "Private"; };
	
	//*** Protected members; only accessible within this class and derived classes ***//
	
	//To make a member protected, use either getters & setters (preferred, but IE doesn't support them) or
	// public functions
	
	var protectedVar = "Protected";
	function getProtectedVar()
	{
		//if the calling function is a derived class
		if(getProtectedVar.caller.prototype instanceof BaseClass) return protectedVar;
		var undef; return undef;
	}
	function setProtectedVar(v)
	{
		//if the calling function is a derived class
		if(setProtectedVar.caller.prototype instanceof BaseClass) protectedVar = v;
	}
	//using getters & setters:
	/*this.__defineGetter__("protectedVar", getProtectedVar);
	this.__defineSetter__("protectedVar", setProtectedVar);*/
	//using public functions; these are publicly defined, but only take action for derived classes:
	this.getProtectedVar = getProtectedVar;
	this.setProtectedVar = setProtectedVar;
	
	//*** Public members ***//
	
	this.toString = function(){ return "[object BaseClass]"; };	//override inherited toString() method
	
	this.publicVar = "Public";
	this.publicFunc = function(){ return "Public"; };
}
//static method of BaseClass class; this is not inherited (it's like String.fromCharCode())
BaseClass.staticMethod = function(){ return "Static"; };


//subclass; inherits BaseClass
function SubClass(arg1, arg2)
{
	BaseClass.apply(this, [arg1, arg2]);	//construct this object using the BaseClass constructor
	
	//*** Private, Protected, and Public members specific to SubClass ***//
	
	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

Revision: 8086
at September 2, 2008 11:49 by wizard04


Updated Code
function Dog(name)	//Dog base class; inherits Object
{
	this.toString = function(){ return "[object Dog]"; };	//override inherited toString() method
	
	this.name = name;
	this.bark = function(){ alert("Ruff!"); };
}
//static method of Dog class; this is not inherited
Dog.breeds = function(){ /*return ["beagle", "labrador", "border collie", ...];*/ };

function Beagle(name)	//Beagle sub-class; inherits Dog class
{
	Dog.apply(this, [name]);	//construct this object as a Dog
	
	this.toString = function(){ return "[object Beagle]"; };	//override inherited toString() method
	
	this.find = function(obj){ alert("Being a hound, finding a(n) "+obj); };
}
Beagle.prototype = new Dog();	//put Beagle into the prototype chain under Dog

Revision: 8085
at September 2, 2008 11:24 by wizard04


Updated Code
function Dog(name)	//Dog base class; inherits Object
{
	this.toString = function(){ return "[object Dog]"; };	//override inherited toString() method
	
	this.name = name;
	this.bark = function(){ alert("Ruff!"); };
}
//static method of Dog class; this is not inherited
Dog.breeds = function(){ /*return ["beagle", "labrador", "border collie", ...];*/ };

function Beagle(name)	//Beagle class; inherits Dog class
{
	Dog.apply(this, [name]);	//construct this object as a Dog
	
	this.toString = function(){ return "[object Beagle]"; };	//override inherited toString() method
	
	this.find = function(obj){ return "Being a hound, finding a(n) "+obj; };
}
Beagle.prototype = new Dog();	//put Beagle into the prototype chain under Dog

Revision: 8084
at September 2, 2008 10:48 by wizard04


Initial Code
function Dog(name, birthdate)	//Dog base class; inherits Object
{
	this.toString = function(){ return "[object Dog]"; };	//override Object's toString method
	
	this.name = name;
	this.bark = function(){ alert("Ruff!"); };
	this.getAge = function()
	{
		var now = new Date();
		var years = now.getFullYear() - birthdate.getFullYear();
		var tmp = new Date(birthdate.getTime());
		tmp.setYear(now.getFullYear());
		if(now.getTime() < tmp.getTime()) years--;	//birthday hasn't occured yet this year
		return years;
	}
}
//static method of Dog class; this is not inherited (it's like String.fromCharCode())
Dog.breeds = function(){ /*return ["beagle", "labrador", "border collie", ...];*/ };

function Beagle(name, birthdate)	//Beagle class; inherits Dog class
{
	Dog.apply(this, [name, birthdate]);	//construct this object as a Dog
	
	//***** Beagle-specific properties and methods (additions and overrides) *****//
	
	this.toString = function(){ return "[object Beagle]"; };	//override Dog's toString method
}
Beagle.prototype = new Dog();	//put Beagle into the prototype chain under Dog

Initial URL


Initial Description


Initial Title
JavaScript Object Inheritance

Initial Tags
javascript, class, object

Initial Language
JavaScript