Any comments welcome: Click here

Well this is some idea that I got after reading a good Javascript book. I always curious is there a way to implement trully private members in Class that I could access from other function defined in this class. Finally I figured out that I can return object that have public defined members as a reference to private ones. How is this work like? Here is an example:

var Man = function(){
 var self = this;
 this.sex = "Male";
 return {
 getSex: function(){
 return this.getSex.apply(self, arguments)
 },
 setSex: function(){
 return this.setSex.apply(self, arguments)
 }
 }
 
};

Man.prototype = {
 getSex: function(){
 return this.sex;
 },
 setSex: function(sex){
 this.sex = sex;
 }
 
}

Let's analyse that! I create simple function that got private members defined on 'this'. We return only public methods (or variables) that receives 'this' as a scope. Then we create .prototype for those methods. What is really cool there? Well - imagine that you have 10000 Objects of Man. Most of known Javascript Class implementation will define whole methods for every single object. This implementation will make sure that methods are reused trough whole class and uses private variable that cannot be accessed any other way outside Class. As you see this implementation does not have full support for other OO functionality now - I just wanted to show whole Idea. Just let me know what you think about this.

Here is short implementation of Class:

var Class = function(classDefinition){
 var proto = {};
 var publicElements;

 /**
 * define prototype, and public members once - at Class creation time
 */
 for (var key in classDefinition) switch (key)
 {
 case "Public":
 publicElements = classDefinition[key];
 break;
 default:
 if (typeof classDefinition[key]==="function") proto[key]=classDefinition[key];
 break;
 }
 
	
	/**
	 * We will return temporary function that will be executed every time we call "new Element" 
	 */
 var temporaryFunction = function(){
 var self = this;
 var returnedElement = {};

		/**
		 * Set all variables on this (except function)
		 */
 for (var i in classDefinition) 
 if (classDefinition.hasOwnProperty(i)) 
 if (typeof classDefinition[i] !== "function") 
 this[i] = classDefinition[i];
 
		/**
		 * Now set public variables, and functions.
		 * Every Function gets passed 'this' argument
		 */
 for (var j = 0; j < publicElements.length; j++) {
 if (proto[publicElements[j]]) 
 returnedElement[publicElements[j]] = (function(what){
 return function(){
 return this[what].apply(self, arguments)
 }
 })(publicElements[j]);
 else 
 returnedElement[publicElements[j]] = self[publicElements[j]];
 }
 return returnedElement;
 
 };
 temporaryFunction.prototype = proto;
 return temporaryFunction;
}