-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmixins_superclassing.html
More file actions
76 lines (69 loc) · 1.96 KB
/
Copy pathmixins_superclassing.html
File metadata and controls
76 lines (69 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Mixins/Superclass Pattern Example</title>
</head>
<body>
<h1>Mixins/Superclass Pattern Example</h1>
<h2>Car Animator</h2>
<button onclick="animateCar()">Animate Car</button>
<div id="carOutput"></div>
<hr />
<h2>Person Animator</h2>
<button onclick="animatePerson()">Animate Person</button>
<div id="personOutput"></div>
<script>
// Mixin definition
const MyMixins = superclass =>
class extends superclass {
moveUp() {
console.log("move up");
}
moveDown() {
console.log("move down");
}
stop() {
console.log("stop! in the name of love!");
}
};
// CarAnimator class
class CarAnimator {
moveLeft() {
console.log("move left");
}
}
// PersonAnimator class
class PersonAnimator {
moveRandomly() {
/*...*/
}
}
// MyAnimator class extending CarAnimator with mixins
class MyAnimator extends MyMixins(CarAnimator) {}
// Create a new instance of MyAnimator
const myAnimator = new MyAnimator();
// Animate Car function
function animateCar() {
const output = document.getElementById("carOutput");
output.innerHTML = "";
myAnimator.moveLeft();
myAnimator.moveDown();
myAnimator.stop();
output.innerHTML = `
<p>Car animation completed. Check the console for details.</p>
`;
}
// Animate Person function
function animatePerson() {
const output = document.getElementById("personOutput");
output.innerHTML = "";
const personAnimator = new PersonAnimator();
personAnimator.moveRandomly();
output.innerHTML = `
<p>Person animation completed. Check the console for details.</p>
`;
}
</script>
</body>
</html>