Mixin Pattern in JavaScript
Level 5289 keystrokes
Builds mixins as functions that return a subclass, then stacks them to add walk and swim to one class.
JavaScript code
// A mixin is a function that takes a base class and returns a subclass with extra methods1const Walks = (Base) => class extends Base {2 walk() { return this.name + " walks"; }3};4const Swims = (Base) => class extends Base {5 swim() { return this.name + " swims"; }6};// Stacking the two mixins gives Duck both walk() and swim()7class Duck extends Swims(Walks(Object)) {8 name = "Duck";9}10const d = new Duck();// Prints Duck walks Duck swims11console.log(d.walk(), d.swim());Output
Duck walks Duck swims
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
(× 10)× 10;× 7"× 6=× 6.× 5{× 5}× 5+× 2>× 2