Factories

by plus5keen

JavaScript

var extend = function(target, source) {
    if (source) {
        for (var key in source) {
            target[key] = source[key];
        }
    }

    return target;
};

var createClass = function(options) {
    if (options == null) {
        return function() {};
    }

    var Class = options.init || function() {};

    var prototype = options.prototype;
    if (prototype == null) {
        var BaseClass = options.BaseClass;
        if (BaseClass != null) {
            prototype = new BaseClass();
        }
    }
    if (prototype != null) {
        extend(prototype, Class.prototype);
        Class.prototype = prototype;
    }

    extend(Class.prototype, options.methods);
    extend(Class, options.staticMethods);

    return Class;
};

var Factory = createClass({
    init: function(options) {
        this.Class = createClass(options);
    },
    methods: {
        create: function(options) {
            // TODO
        }
    }
});

var createFactory = function(options) {
    return new Factory(options);
};

console.log(new Factory);