QuimbleApi : JavascriptTechnique

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
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]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.1
Page was generated in 1.8884 seconds