-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_3.js
53 lines (42 loc) · 891 Bytes
/
class_3.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
42
43
44
45
46
47
48
49
50
51
52
53
class Robot {
constructor(name) {
this.name = name;
}
move() {
console.log(`${this.name} is moving`);
}
}
const r0 = new Robot('Pepper');
r0.move();
class Weapon {
constructor(description) {
this.description = description;
}
fire() {
console.log(`${this.description} is firing`);
}
}
const w0 = new Weapon("pew pew laser");
w0.fire();
class CombatRobot extends Robot {
constructor(name) {
super(name);
this.weapons = [];
}
addWeapon(weapon) {
this.weapons.push(weapon);
}
fire() {
console.log("firing all weapons");
this.weapons.forEach(weapon => weapon.fire());
}
}
const r1 = new CombatRobot("Optimus Prime");
r1.move();
r1.addWeapon(w0);
r1.fire();
Robot.prototype.fly = function () {
console.log(`${this.name} is flying`);
}
r0.fly();
r1.fly();