Composition tends to bloat code and can become unreadable within large projects, however DRY is a must. Most tutorials and articles covert classes to functions and compose functionalities. One other added those functionalities to a Class prototype which is also awesome, why not directly assign it as a method to that class then?
The aim is to keep classes and at the same time benefit from composition with the help of modules.
Approach 1? Why?
const prepare = () => {
return {
prepare: () => console.log("Preparing..."),
};
};
const ready = () => {
return {
ready: () => console.log("Ready!"),
};
};
const createPizza = () => {
const pizza = {
....
}
return {
...pizza,
...prepare(),
...ready()
}
}
const pipi = createPizza();
pipi.prepare();
Approach 2? Why not?
const prepare = () => console.log("Preparing...");
const ready = () => console.log("Ready!");
class Pizza {
}
Pizza.prototype.prepare = prepare;
Pizza.prototype.ready = ready;
Approach 3? why not?
const prepare = () => console.log("Preparing...");
const ready = () => console.log("Ready!");
class Pizza {
prepare() {
prepare();
}
ready() {
ready();
}
}
Why wouldn’t I follow approach 3, does it violate other design patterns? how?
Isn’t both approaches of 2 and 3 more memory efficient here?
All approaches give the same results and perhaps approaches 2 and 3 are more memory efficient.