(please note this is not a duplicate question, see @Jeff M's answer below)
I am trying to understand es6 class syntax. I want to compose my classes in the same way as this code does using factory functions:
Example 1:
const barker = (state) => ({
bark: () => console.log("Woof, I am " + state.name)
});
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
});
const murderRobotDog = (name) => {
let state = {
name,
speed: 100,
position: 0
};
return Object.assign({},
barker(state),
driver(state)
);
};
const bruno = murderRobotDog("bruno");
bruno.bark(); // "Woof, I am Bruno"
My working example of class composition using 'class' from a completely different tutorial looks like this:
Example 2:
class Employee {
constructor(firstName, familyName) {
this._firstName = firstName;
this._familyName = familyName;
}
getFullName() {
return `${this._firstName} ${this._familyName}`;
}
}
class Group {
constructor(manager /* : Employee */ ) {
this._manager = manager;
this._managedEmployees = [];
}
addEmployee(employee) {
this._managedEmployees.push(employee);
}
}
I can see how the concept of composition works, but these seem like very different ways of doing it. The first example using the factories seems the most intuitive, but I want to use the es6 class syntax (don't worry why :)
Maybe I am missing something and the answer is obvious, but how would I do exactly what is done in example 1 using es6 classes in the most simplistic way? THANKS.