My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

jClass is a Java Script library that allows you to write highly object-oriented code.

You can use jClass to:

  • Define namespaces
  • Create and extend other classes
  • Create and implement interfaces
  • Define public/private/static/override/overload methods
  • Define getter and setter methods

The basic syntax for jClass is:

include("some.other.files");

namespace("my.namespace")
({
    MyClass : jClass.extends(BaseClass).implements(SomeInterf, SomeOtherInterf) 
    ({
        constructor : function(){ 
         .... 
        },
        private : {
          ....
        },
        public : {
          ....
        },
        static : {
          ....
        }
    }),
    OtherClass : jClass({
       ......
    })
})

A set of really useful libraries are also included. You can check them out in the Wiki

And a demonstration example:

File : libraries/adm/classes.js

namespace("adm.classes")
({
     // Defining a interface. Implement bodies for the methods here will throw an error
    ITestInterface = jInterface({
        add : function(name) {}
        remove : function() {}
    }),

    // Defining a class to extend later
    BaseClass = jClass({
        public : {
            add : function(name) {
                users.push(name);
            } 
            getUsers: function() {
                // returns reference to private variable users
                return users; 
            }
        },
        private : {
            // private variable, no one except this class can access this
            users : ['John', 'Bill']
        },
        static : {
            BASE_CLASS_STATIC : "i'm from base class"
        }
    })
});

File : index.html

include("adm.classes");

var MainClass = jClass.extend(adm.classes.BaseClass).implement(adm.classes.ITestInterface)({
     constructor : function() {
           // class constructed
           localCopy = _super.getUsers();
     },
     public : {
         // Not implementing this will throw and error because it's required by the interface
         remove : Overload(
                     function(name) { // delete user by name  
                         delete localCopy[localCopy.indexOf[name]];
                     },
                     // overloading function remove to delete users between start and end positions
                     function(start, end) {
                         localCopy.splice(start, end);
                     }
                  ),
          // Overiding baseclass function add()
          add : function(name) {
              _super.add(name);
              this.doSometing();
          },
          doDomething : function() {
               // does something
          },
          total : GetSet(function() {
                    return _total; // set total
                  },
                  function(val) {
                     _total = val; // get total 
                     this.doSomething();
                  })
     }, 
     private : {
        localCopy : []
     },
     static : {
        SOMETHING_STATIC : 123
     }
});

var obj = new MainClass();

obj.isInstanceOf(adm.classes.MainClass); // true
obj.isInstanceOf(BaseClass); // true
obj.isImplmentationOf(adm.classes.ITestInterface); // true

MainClass.SOMETHING_STATIC; // 123
MainClass.BASE_CLASS_STATIC; // inherited from base class "i'm from base class"

obj.total = 1; // calling setter: set's new value to _total and calls doSomething()
obj.total; // calling getter: returns value of _total (1)

obj.add('Tom'); // adds user Tom

obj.remove('Bill'); // removes user by name
obj.remove(0, 3); // removes users between 0-3
Powered by Google Project Hosting