<jga.me/>

Fork me on GitHub

Aug - a library to augment or extend your javascript objects

If you've used jQuery before, you are probably familiar with the extend function. If you need to be reminded... the gist of it is that you can extend an object with another object. For example, if you had a plugin that had some defaults (delay: 3000, auto: true) and you wanted to allow for developers to override them (delay: 5000), you could take in an options object and combine the two objects (delay: 5000, auto: true). Aug is a tiny (354 bytes) library that will let you do that without jQuery. I've modeled the syntax to be identical to jQuery. I've also added the ability to augment javascript classes (adding to the prototype). Here are some examples:

Examples

//Great for overriding defaults
var a = { a: 1 };
var b = { a: 2, b: 3 };
aug(a, b); //a = { a: 2, b: 3};

//Combining two objects without overriding
var a = { a: 1 };
var b = { b: 3 };
var c = aug({}, a, b); //c = { a: 1, b: 3};

//Extending a prototype
var Class1 = function() { };
Class1.prototype.test = function() {
        return 0;
};

aug(Class1, {
        test2: function() {
                return 5;
        }
});
var c = new Class1();
c.test(); //returns 0;
c.test2(); //returns 5

Playgroud

If you want to try out aug. Check out this jsfiddle.

Downloads

You can grab the library here or check out the source in github.

For you nerds, the library has AMD and node.js support.

Node

npm install aug
Any thoughts?
blog comments powered by Disqus

Categories

Recent Posts