JavaScript, jquery

Object Oriented JavaScript Code Template with Scope, namespace, and inheritance


Object Oriented JavaScript Code Template with Scope, namespace, and inheritance

12/30/2009

It took me some time to compile the features of the Object Oriented language such as C# into one JavaScript Template bundle. Here is what I came up with. This code template supports the following features:

Class block cope
NameSpace importing (“using” in C#) and dependancy checking on load
Privat string
Public string method
Privat void
Public Class (can have instances)
BaseClass / SubClass inheritance

//base class closure
(function($){
   
   //Createst Namespace Object
    if (!window.MyNamespace) {
        window.MyNamespace = {}
    };

    //Createst local shortuct
    var m = window.MyNamespace; 
   
    m.baseClass = {
        baseClassMethod : function(var1, var2){
            return var1 + var2;
        }
    };

})(jQuery); //importing JavaScript framework


//sub class closure
(function($, baseClass){
   
   //Createst Namespace Object
    if (!window.MyNamespace) {
        window.MyNamespace = {}
    };

    var privatSettign = {
        set1 : "test",
        set2 : "test"
    };
   
    var privatString = function(){
        return privatSettign.set1;
    }

    var privatVoid = function(){
        
    }();

    //class definition
    window.MyNamespace.myClass = {}; 
    
    //Createst local shortuct
    var m = window.MyNamespace.myClass

    //enherit from base class
    $.extend(m, baseClass);

    //adding methods:
    
    m.publicStringMethod1 = function(){
        
        //use pirvat class like this
        var a = privatString();

        return a;
    }
        
    m.publicStringMethod2 = function(){
        
        //use public same object class like this
        this.publicStringMethod1();
        
        var localVar;
        
        var anotherPrivatVoid = function(){
            localVar = privatSettign.set1 + " " + privatSettign.set2;
        }();
        
        //call base class method:
        var added = this.baseClassMethod("resut is: ", localVar);
        
        return added;
    }
        
    m.publicInstanceMethod = function(){
        
        var privatVar; 
        
        //call another method 
        privatVar = this.publicStringMethod2()

        return {
            getVar : function(){
                privatVar;
            },
            setVar : function(_var){
                privatVar = _var;
            },
            alertVar : function(_var){
                alert(privatVar);
            }
        };
    }

})(jQuery, MyNamespace.baseClass); //importing JavaScript framework and class dependancy



//class usage:
(function($, myClass){

    //now sub class is available directly so we can call it
    var someVar = myClass.publicStringMethod1;
    
    //creating instnaces
    var inst = myClass.publicInstanceMethod();
    
    //baseClass method:
    var baseVar = myClass.baseClassMethod(1, 2);
    
    //alert original
    inst.alertVar();
    
    //lets now set some var
    inst.setVar("some var" + baseVar);
    
    //do something with the var
    inst.alertVar();

     
})(jQuery, MyNamespace.myClass); //importing JavaScript framework and class dependancy

Responses to The Object Oriented JavaScript Code Template with Scope, namespace, and inheritance

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s