0.8.0 • Published 10 years ago

classjs v0.8.0

Weekly downloads
6
License
-
Repository
github
Last release
10 years ago

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);
0.8.0

10 years ago

0.7.1

11 years ago

0.7.0

12 years ago

0.6.2

12 years ago

0.6.1

12 years ago

0.6.0

12 years ago

0.5.2

12 years ago

0.5.1

12 years ago

0.5.0

12 years ago

0.2.0

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago