Search⌘ K
AI Features

Solution Review: Let's Make a Burger

Explore how to use mixins in JavaScript to combine methods from multiple classes into one, like the Burger class example. Understand the role of the combineClasses function to dynamically add methods, ensuring new classes' methods are seamlessly incorporated without inheritance.

We'll cover the following...

Solution #

Javascript (babel-node)
class Vegetables {
veggies() {
return "Choose Veggies"
}
}
class Meat {
meat() {
return "Choose Meat"
}
}
class Sauces{
choosingSauces(){
return "Choose Sauces"
}
}
function combineClasses(dest,...src){
for (let _dest of src) {
for (var key of Object.getOwnPropertyNames(_dest.prototype)) {
dest.prototype[key] = _dest.prototype[key]
}
}
}
class Burger{
}
//adding a new class
class Cheese{
addingCheese(){
return "Add Cheese"
}
}
combineClasses(Burger,Vegetables,Meat,Sauces,Cheese)
var burger = new Burger()
console.log(burger.veggies())
console.log(burger.meat())
console.log(burger.choosingSauces())
console.log(burger.addingCheese())

Explanation #

The challenge had the following requirements:

  • The methods of all classes should be available to the Burger class.

  • If any new classes are added, their methods should also be available to Burger class.

How do we ...