Skip to content

Commit 2f645e3

Browse files
authored
Merge pull request #306 from usernamehw/patch-5
Update article.md
2 parents 6cb0a55 + 949765e commit 2f645e3

File tree

1 file changed

+9
-9
lines changed
  • 1-js/07-object-oriented-programming/13-mixins

1 file changed

+9
-9
lines changed

1-js/07-object-oriented-programming/13-mixins/article.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

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`.
66

77
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.
88

@@ -24,10 +24,10 @@ For instance here the mixin `sayHiMixin` is used to add some "speech" for `User`
2424
*/!*
2525
let sayHiMixin = {
2626
sayHi() {
27-
alert("Hello " + this.name);
27+
alert(`Hello ${this.name}`);
2828
},
2929
sayBye() {
30-
alert("Bye " + this.name);
30+
alert(`Bye ${this.name}`);
3131
}
3232
};
3333

@@ -44,7 +44,7 @@ class User {
4444
Object.assign(User.prototype, sayHiMixin);
4545

4646
// now User can say hi
47-
new User("Dude").sayHi(); // Hi Dude!
47+
new User("Dude").sayHi(); // Hello Dude!
4848
```
4949

5050
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 = {
7575
*!*
7676
// call parent method
7777
*/!*
78-
super.say("Hello " + this.name);
78+
super.say(`Hello ${this.name}`);
7979
},
8080
sayBye() {
81-
super.say("Bye " + this.name);
81+
super.say(`Bye ${this.name}`);
8282
}
8383
};
8484

@@ -138,8 +138,8 @@ let eventMixin = {
138138
off(eventName, handler) {
139139
let handlers = this._eventHandlers && this._eventHandlers[eventName];
140140
if (!handlers) return;
141-
for(let i = 0; i < handlers.length; i++) {
142-
if (handlers[i] == handler) {
141+
for (let i = 0; i < handlers.length; i++) {
142+
if (handlers[i] === handler) {
143143
handlers.splice(i--, 1);
144144
}
145145
}
@@ -183,7 +183,7 @@ let menu = new Menu();
183183

184184
// call the handler on selection:
185185
*!*
186-
menu.on("select", value => alert("Value selected: " + value));
186+
menu.on("select", value => alert(`Value selected: ${value}`));
187187
*/!*
188188

189189
// triggers the event => shows Value selected: 123

0 commit comments

Comments
 (0)