classjs

Simple Class System for JavaScript

class.js

Simple Class System for JavaScript. Designed to work with backbone.js and node.js.

  • Inheritance
  • Mixins
  • Interfaces
  • Properties
    • getter / setter
    • visibility
    • type check (Class, Object, String, Number, Boolean, Function, Array, Element, Regex)
    • format value
    • validation
    • init value
    • events (optional, event emitter needed: e.g. Backbone.Events, node.js events.EventEmitter)
  • super - reference on super class
  • Singleton
  • Browser & commonJS support
  • http://underscorejs.org

Browser:

<script src="unsercore.js"></script>
<script src="class.js"></script>

node.js:

npm install classjs
require("classjs");
var Monkey = Class.define(EventEmitter, {
    properties : {
        "legs" : {
            type : "Number",
            init : 4,
            validate : function(valueold) {
                return value <= 4;
            },
            apply : "_applyLegs",
            event : "legsChanged"
        },
 
        head : {
            type : "Object",
            get : false, // generate only private getter / setter 
            set : false
        }
    },
 
    members : {
        _applyLegs : function(valueold) {
            console.log(value, old);
        },
        
        jump : function() {
            console.log("jump");
        }
    }
});
 
var TwoLeggedMonkey = Monkey.extend({
    constructor : function() {
        this.__super__.apply(this, arguments);
        this.setLegs(3, false); // Do not fire the event 
    },
    
    jump : function() {
        this.__super__.jump.apply(arguments);
    }
});
 
var monkey = new TwoLeggedMonkey();
monkey.on("legsChanged", function(data) {
    console.log("Legs changed to %i", data.value);
});
monkey.jump();
monkey.setLegs(2);