node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…


Arale Class and Events


This is a fork of arale.class and The core of arale projects.

The Class and Events of Arale is very easy to use, and stable now. Test cases with 97% coverage, and you don't have to worry about the 3% left.

Read it in Chinese at Arale.Class.

Class.create([parent], [properties]) create a new class:

/* pig.js */
var Class = require('arale').Class;
var Pig = Class.create({
    initialize: function(name) { = name;
    talk: function() {
        console.log('I am' +;
module.exports = Pig;

Every class created by Class.create has an extend method:

/* red-pig.js */
var Pig = require('./pig');
var RedPig = Pig.extend({
    initialize: function(name) {, name);
    color: 'red'
module.exports = RedPig;

You can also mixin other classes:

/* flyable.js */ = function() {
    console.log('I am flying');
/* flyable-red-pig.js */
var RedPig = require('./red-pig');
var Flyable = require('./flyable');
var FlyableRedPig = RedPig.extend({
    Implements: Flyable,
    initialize: function(name) {, name);
module.exports = FlyableRedPig;

The other way of mixin:

/* flyable-red-pig-extension.js */
var FlyableRedPig = require('./flyable-red-pig');
   swim: function() {
       console.log('I can swim');

Transfrom a function to class:

function Animal() {
} = function() {};
var Dog = Class(Animal).extend({
    swim: function() {}

Read it in Chinese at Arale.Events.

There are two ways of implementing events:

var Events = require('arale').Events;
var object = new Events();
object.on('expand', function() {

The other way:

var Events = require('arale').Events;
function Dog() {
Dog.prototype.sleep = function() {
var dog = new Dog();
dog.on('sleep', function() {
    console.log('the dog is sleeping');

object.on(event, callback, [context])

You can pass a context to change the this.

post.on('saved', callback, that);

There is an all event:

proxy.on('all', function(eventName) {
});[event], [callback], [context])

// remove the onChange callback of change event'change', onChange);
// remove all callbacks of change event'change');
// remove all onChange callbacks of every event, onChange);
// remove all events for the context, null, context);
// remove all events of the object;

object.trigger(event, [*args])

var obj = new Events();
obj.on('x y', fn);
// equals: