Advertisement
728x90
javascript
1// Define a mixin
2
3const canEat = {
4 eat() {
5 console.log("Eating...");
6 }
7};
8
9const canWalk = {
10 walk() {
11 console.log("Walking...");
12 }
13};
14
15const canRead = {
16 read() {
17 console.log("Reading...");
18 }
19};
20
21// Create a class
22
23class Person {
24 constructor(name) {
25 this.name = name;
26 }
27}
28
29// Apply mixins
30
31Object.assign(Person.prototype, canEat, canWalk, canRead);
32
33// Use it
34
35const person = new Person("Sudheer");
36
37person.eat(); // Output: Eating...
38
39person.walk(); // Output: Walking...
40
41person.read(); // Output: Reading...Advertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 9
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
352of476