-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.js
41 lines (36 loc) · 1.12 KB
/
Example.js
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
class BallFactory {
constructor(type) {
this.createBall = function(type) {
let ball;
if (type == "football" || type === "soccer") ball = new Football();
else if (type === "basketball") ball = new Basketball();
ball.roll = function() {
return `The ${this._type} is rolling.`;
}
return ball;
}
}
}
class Football {
constructor() {
this._type = 'football';
this.kick = function() {
return 'You kicked the football.';
}
}
}
class Basketball {
constructor() {
this._type = 'basketball';
this.kick = function() {
return 'You kicked the basketball.';
}
}
}
const factory = new BallFactory();
const myFootball = factory.createBall('football');
const myBasketball = factory.createBall('basketball');
console.log(myFootball.roll()); // The football is rolling.
console.log(myBasketball.roll()); // The basketball is rolling.
console.log(myFootball.kick()); // You kicked the football.
console.log(myBasketball.bounce()); // You bounced the basketball.