node package manager

luma

A thin layer of JS OO sugar

Luma

JavaScript's native OO utilties don't need much neatening up; but it has some rough edges. Luma smooths them off; by doing as little as possible.

Walkthrough

constructors

The main rough point that luma cleans up is the making of constructor functions.

    
    // instead of this:
    var Person = function(name){
        this.name = name
    }
 
    Person.prototype = {
        greet: function(){
            return 'hi, I\'m ' + this.name
        }
    }
 
    var hugh = new Person('hugh')
 
    // we have:
    var person = luma.factory({
        init: function(name){
            this.name = name
            return this
        }
      , greet: function(){
            return 'hi, I\'m ' + this.name
        }
    })
 
    var hugh = person('hugh')

As with constructor functions, person.prototype will be the [[Prototype]] property of objects created.

There are a number of reasons for this design:

  • With the init function, any applicable value can be returned; whereas, with the new operator, only an object can be (otherwise, the object new implicitly creates is returned);
  • Avoids clobbering the namespace through a forgotten new in non-strict mode code.
  • no two-stage initialisation required for basic constructor creation.

creating single objects

Another rough edge to JS' OO is Object.create. Luma neatens it up by replacing property descriptor with object(s) to be mixed in to the created object:

    // instead of: 
    var person = { 
            greet: function(){
                return 'hi, I\'' + this.name
            }
        }
      , hugh = Object.create(person)
 
    hugh.name = 'hugh'
 
    // we have: 
    var person = { 
            greet: function(){
                return 'hi, I\'' + this.name
            }
        }
      , hugh = luma.create(person, { name: 'hugh' })

You can mixin any number of objects this way. Let's imagine you want to create a person who also has Backbone's Event mixin:

    var hugh = luma.create(person, Backbone.Events, { name: 'hugh' })

API

luma.factory

    factory :: (proto) -> function

Creates an informal constructor (one that doesn't require new) function from a object (which is used as a prototype). If the prototype has an init method available on it, it will be called with the arguments passed to the constructor function.

luma.create

    create :: (proto, [source]) -> object

Creates an object, with proto as its [[Prototype]] object. Any source objects will be mixed in to the return object.

luma.extend

    extend :: (destination, source, [extra sources] ) -> object

Same as underscore's extend, it shallow copies all enumerable properties from the source objects to the destination object. When there's a conflict; the rightmost object's property is preferred.


Getting setup

Get via npm:

    npm install luma

Or include as a regular script in the browser:

    <!-- after copying luma.js to your server -->
    <script src="/luma.js"></script> 

License

Luma is under the MIT license.