Pretty cool class creation with public/private functions
var TopClass = function(opts) {
var CloneObject = function(what) {
for (i in what) {
if (typeof what[i] == 'object') {
this[i] = new CloneObject(what[i]);
}
else
this[i] = what[i];
}
};
var extendObject = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
var instanceHandlerMethods = {
//for future enhancement
};
var tc = function() {
extendObject(this, opts.privateMethods);
var instances = new Array;
if(opts.publicInstanceMethods || opts.privateInstanceMethods) {
var newInstance = function() {
var parentClass = {};
var initVariables;
var instanceId;
extendObject(this, opts.privateInstanceMethods);
return extendObject(opts.publicInstanceMethods, instanceHandlerMethods);
}();
return extendObject(opts.publicMethods, {
create: function(initVariables) {
if(typeof(onBeforeInstanceCreate) == "function") {
onBeforeInstanceCreate(initVariables);
}
var returnedInstance = new CloneObject(newInstance);
returnedInstance.parentClass = this;
returnedInstance.initVariables = initVariables;
returnedInstance.instanceId = instances.push(returnedInstance);
if(typeof(returnedInstance.onAfterCreate) == "function") {
returnedInstance.onAfterCreate();
}
if(typeof(onAfterInstanceCreate) == "function") {
onAfterInstanceCreate(returnedInstance);
}
return returnedInstance;
}
});
} else {
return opts.publicMethods;
}
}();
if(typeof(tc.onAfterCreate) == "function") {
tc.onAfterCreate();
}
return tc;
};
There are 3122 comments on this page. [Display comments]