o.js

JavaScript utility belt for rich objects and prototypes. Ooooh!

Powerful

If you like the rich OO features of other languages, such as Perl and Java, and wish you had the same power in JavaScript, but with a small footprint, then o.js is for you.

Compatible

o.js is designed to extend JavaScript's native prototype system, not replace it. This means that o.js classes, traits, attributes, types, and objects are automatically compatible with existing JavaScript libraries.

Flexible

o.js is built of components that do one thing and do it well, providing a rich library of utilities that build upon each other. Any layer of the o.js stack of components may be used independently as needed.

Download

License

o.js may be freely distributed under the MIT license.

Classes

Define classes using new o.Class(...) which creates a standard JavaScript function constructor. A class can have attributes, methods, method overrides, and a number of other features. A class, at its core (take a look at the source code) is a very simple wrapper around a trait.

var Person = new o.Class({
    attributes: {
        name: { type:o.nonEmptyStringType },
        age: { type:o.integerType }
    },
    methods: {
        log_name: function () {
            console.log( this.name() );
        }
    }
});
var baggins = new Person({
    name: 'Bilbo',
    age: 111
});
baggins.log_name();

Read more in the o.Class documentation.

Traits

Traits are created with new o.Trait(...) which creates a new Trait object. Traits can be included in Classes or installed directly on to existing objects.

var productTrait = new o.Trait({
    attributes: {
        price: { type:o.numberType }
    }
});
var Sheet = new o.Class({
    attributes: {
        threadCount: { type:o.integerType }
    },
    traits: [ productTrait ]
});
var mySheet = new Sheet({
    price: 24.95,
    threadCount: 160
});

Read more in the o.Trait documentation.

Attributes

Attribute can be created directly via new o.Attribute(...) and re-used, or inderectly in Class and Trait definitions.

var fullNameAttr = new o.Attribute({
    type: o.nonEmptyStringType,
    required: true
});

Read more in the o.Attribute documentation.

Types

Type objects validate and coerce attribute arguments. New types can be created via new o.Type(...) and used in Attribute definitions.

var ageType = new o.AllType([
    o.integerType,
    o.positiveType
]);

Read more in the Types and o.Type documentation.

Plumbing

A rich array of plumbing functions exist which provide the moving pieces that make Classes, Traits, Attributes, and Types work. Plumbing functions include o.construct(), o.augment(), o.merge(), o.clone(), o.has(), o.before(), o.after(), o.around(), etc.

Read more in the Plumbing documentation.