I'm working on a utility to create classes in javascript. And it works, the problem is how to define private attributes.
This is the code
var OO = {
Class:function(){
var len = arguments.length;
var data = arguments[len-1];
var Klass;
if (data.constructor === Object){
Klass = function (){};
} else {
Klass = data.constructor;
delete data.constructor;
}
OO.extend(Klass.prototype,data); //Classic Extend Method
return Klass;
},
//Simple extend method, just what I need in this case
extend: function(target, source){
var prop;
for (prop in source)
target[prop] = source [prop];
}
}
This is how it works
// first create a class
var person = OO.Class ({
constructor: function (name, age) {
this.name = name;
this.age = age;
},
name:'',
age:'',
getName: function () {
return this.name;
},
getAge: function () {
return this.age;
}
});
And here is the instance
var one = new Person ('josh', 22);
And the problem:
one.age / / returns 22
one.name / / returns josh
What I need is that these properties can only be accessed by methods like getName () and getAge ()
EDIT1: Added Extend Function