You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/07-object-oriented-programming/13-mixins/article.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
In JavaScript we can only inherit from a single object. There can be only one `[[Prototype]]` for an object. And a class may extend only one other class.
4
4
5
-
But sometimes that feels limiting. For instance, I have a class `StreetSweeper` and a class `Bycicle`, and want to make a `StreetSweepingBycicle`.
5
+
But sometimes that feels limiting. For instance, I have a class `StreetSweeper` and a class `Bicycle`, and want to make a `StreetSweepingBicycle`.
6
6
7
7
Or, talking about programming, we have a class `Renderer` that implements templating and a class `EventEmitter` that implements event handling, and want to merge these functionalities together with a class `Page`, to make a page that can use templates and emit events.
8
8
@@ -24,10 +24,10 @@ For instance here the mixin `sayHiMixin` is used to add some "speech" for `User`
24
24
*/!*
25
25
let sayHiMixin = {
26
26
sayHi() {
27
-
alert("Hello "+this.name);
27
+
alert(`Hello ${this.name}`);
28
28
},
29
29
sayBye() {
30
-
alert("Bye "+this.name);
30
+
alert(`Bye ${this.name}`);
31
31
}
32
32
};
33
33
@@ -44,7 +44,7 @@ class User {
44
44
Object.assign(User.prototype, sayHiMixin);
45
45
46
46
// now User can say hi
47
-
newUser("Dude").sayHi(); //Hi Dude!
47
+
newUser("Dude").sayHi(); //Hello Dude!
48
48
```
49
49
50
50
There's no inheritance, but a simple method copying. So `User` may extend some other class and also include the mixin to "mix-in" the additional methods, like this:
@@ -75,10 +75,10 @@ let sayHiMixin = {
75
75
*!*
76
76
// call parent method
77
77
*/!*
78
-
super.say("Hello "+this.name);
78
+
super.say(`Hello ${this.name}`);
79
79
},
80
80
sayBye() {
81
-
super.say("Bye "+this.name);
81
+
super.say(`Bye ${this.name}`);
82
82
}
83
83
};
84
84
@@ -138,8 +138,8 @@ let eventMixin = {
138
138
off(eventName, handler) {
139
139
let handlers =this._eventHandlers&&this._eventHandlers[eventName];
0 commit comments