JSFiddle - React, Tailwind, and code Playground
by Leo Cavalcante
JavaScript
const swimmer = state => ({
swim() {
console.log(`${state.name} is swimming`);
}
});
const cyclist = state => ({
cycle() {
console.log(`${state.name} is cycling`);
}
});
const runner = state => ({
run() {
console.log(`${state.name} is running`);
}
});
const triathlete = name => {
const state = {
name
};
return Object.assign({
run() {
console.log('Ill be overitten');
}
},
// extends feelling?
swimmer(state),
cyclist(state),
runner(state), {
doIt() {
console.log(`${state.name} is doing it`);
this.swim();
this.cycle();
this.run();
}
}
);
};
const athletes = [triathlete('Bob'), triathlete('John')];
athletes.forEach(athlete => athlete.doIt());
// pitfalls about no using classes
console.log(athletes.map(athlete => typeof athlete)); // ["object", "object"]