Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Classes and Modules for Underscore.js
tree: ff4063b337

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
events.js
readme.textile
underoop.js

readme.textile

Introduction to Underoop.js

Underoop.js is a Underscore.js mixin that adds some handy functions for OOP style development. This style of development is particulary useful when bulding extensible loosely coupled components that are consistently and logically structures around some data. I also wanted to create somethig that would work well with Underscore’s functional operations on JavaScript Objects and Arrays.

Defining new Classes

Often you want OOP lass-like things but writing your objects in the following manner is kind of tedious:

var MyClass = function() {
    this.foo = "foo";
    this.bar = "bar";
};

MyClass.prototype.baz = function() {
    ...
}

MyClass.prototype.woz = function() {
    ...
}

Using Underoop.js you can instead write the following:

var MyClass = _.Class({
    initialize: function() {
       this.foo = "foo";
       this.bar = "bar";
    },

    baz: function () {
    },

    woz: function() {
    }
});

Defining Modules

Often you’ll have a chunk of functionality that does not belong to a particular class. You can define a set of methods like so:

var Events = _.Module({
    addEvent: function(type, fn) { ... },
    removeEvent: function(type, fn) { ... },
    removeEvents: function(type) { ... },
    fireEvent: function(type) { ... }
});

And “mixin” in this functionality easily:

var Foo = _.Class({
    includes: [Events],
    intialize: function () {
        ...      
    } 
});

This is similar to how Ruby deals with this.

Messaging

Being able to package up a message is pretty useful, especially at cutting down the amount of typing that you have to do. All methods of a Type are added to _sel so you use them with Underscore’s excellent interfaces to the JavaScript collections.

var Foo = _.Class({
  initialize: function(z) {
    this.z = z;
  },
  add: function(x, y) {
    return [x, y].join("");
  }
});
var Bar = _.Class({
  initialize: function(z) {
    this.z = z;
  },
  add: function(x, y) {
    return x + y + thiz.z;
  }
});

var a = new Foo("cool!");
var b = new Bar(6);

_([a, b]).map(_sel.add(4, 5));

Inheritance

Underoop does not support inheritance. Inheritance breaks miserably under AJAX request without a lot of ugly hacks. I’ve come to the conclusion that inheritance is a bug. Use Modules.

Caveats

Name clashes are always a concern between Class method names, Class instance vars, Module method names, and Module instance vars. Underoop can’t solve these problems for you without adding a lot of incidental complexity for and myself. However JavaScript private scope, lookup tables, and Underscore’s excellent _.unqiueId can work wonders ;)

Something went wrong with that request. Please try again.