Snippet #114


/**
 * Class.js: A class factory.
 *
 * Source: https://raw.github.com/gist/1033002/26e77ac9d77841994fb02246f920c2929a2d7513/class.js
 * See also: http://vpalos.com/1194/js-classes-for-the-masses/
 */
function Class(members) {

    // setup proxy
    var Proxy = function() {};
    Proxy.prototype = (members.base || Class).prototype;

    // setup constructor
    members.init = members.init || function() {
        if (Proxy.prototype.hasOwnProperty("init")) {
            Proxy.prototype.init.apply(this, arguments);
        }
    };
    var Shell = members.init;

    // setup inheritance
    Shell.prototype = new Proxy();
    Shell.prototype.base = Proxy.prototype;

    // setup identity
    Shell.prototype.constructor = Shell;

    // setup augmentation
    Shell.grow = function(items) {
        for (var item in items) {
            if (!Shell.prototype.hasOwnProperty(item)) {
                Shell.prototype[item] = items[item];
            }
        }
        return Shell;
    };

    // attach members and return the new class
    return Shell.grow(members);
}
← Snippet #970
Snippet #969 →

All snippets