codeholic codeholic - 4 months ago 17
Javascript Question

JavaScript pattern for multiple constructors

I need different constructors for my instances. What is a common pattern for that?

Answer

How do you find this one?

function Foobar(foobar) {
    this.foobar = foobar;
}

Foobar.prototype = {
    foobar: null
};

Foobar.fromComponents = function(foo, bar) {
    var foobar = foo + bar;
    return new this(foobar);
};
Comments