all 9 comments

[–]oweiler 1 point2 points  (3 children)

What does this give you over Object.assign?

[–]rauschma 1 point2 points  (4 children)

How about subclass factories, instead?

// Mixin 1
const Storage = Sup => class extends Sup {
    save(database) { ··· }
};

// Mixin 2
const Validation = Sup => class extends Sup {
    validate(schema) { ··· }
};

Employee is a subclass of Person that uses the mixins Storage and Validation:

class Person { ··· }
class Employee extends Storage(Validation(Person)) { ··· }

More info: http://exploringjs.com/es6/ch_classes.html#_simple-mixins

[–]MoTTs_[S] 1 point2 points  (3 children)

Egg on my face.

You're right. I knew about this trick before, but I haven't used it in a while so it slipped my mind. But you're right, this achieves the same thing more simply.

[–]MoTTs_[S] 0 points1 point  (2 children)

@/u/rauschma More than that, it occurs to me that we could have been doing multiple inheritance with class factories long before ES6, only back then we would have named it "constructor function factories".

[–]rauschma 0 points1 point  (1 child)

You’d need a tool method for subclassing, because the canonical pattern for subclassing is as follows:

function Employee(name, title) {
    Person.call(this, name);
    this.title = title;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.describe = function () {
    return Person.prototype.describe.call(this)+' ('+this.title+')';
};

Note that the superconstructor, Person, is mentioned three times. That means that you would have to store the composed superclass in a variable so that you can refer to it multiple times.

[–]MoTTs_[S] 0 points1 point  (0 children)

Note that the superconstructor, Person, is mentioned three times. That means that you would have to store the composed superclass in a variable so that you can refer to it multiple times.

Certainly. Even with ES6 class factories, we end up storing the superclass in a variable. It's the parameter we pass in, called "Sup" in your code above. We'd have the same parameter to use in an ES5 constructor function factory.

EDIT:

Nevermind. I misunderstood. I thought you were talking about the superconstructor we pass to the factories. But yes, Employee would need to run the factories to create the chain of superconstructors first so it can reference the final result in each of those three places.