0.8.0 • Published 11 years ago
classjs v0.8.0
class.js
Simple Class System for JavaScript. Designed to work with backbone.js and node.js.
Features
- 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
Dependencies
Usage
Browser:
<script src="unsercore.js"></script>
<script src="class.js"></script>
node.js:
npm install classjs
require("classjs");
Example
var Monkey = Class.define(EventEmitter, {
properties : {
"legs" : {
type : "Number",
init : 4,
validate : function(value, old) {
return value <= 4;
},
apply : "_applyLegs",
event : "legsChanged"
},
head : {
type : "Object",
get : false, // generate only private getter / setter
set : false
}
},
members : {
_applyLegs : function(value, old) {
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);