-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.js
84 lines (69 loc) · 1.46 KB
/
classes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//////////////////////////
// ES6 - Classes
/////////////////////////
/*
// ES5
function Car(options) {
this.title = options.title;
}
Car.prototype.drive = function() {
return 'vroom';
}
function Toyota(options) {
Car.call(this, options);
this.color = options.color;
}
const car = new Car({ title: 'Focus' });
console.log(car.drive());
console.log(car);
Toyota.prototype = Object.create(Car.prototype);
Toyota.prototype.constructor = Toyota;
Toyota.prototype.honk = function() {
return 'Beep';
}
const toyota = new Toyota({ color: 'red', title: 'Daily Driver' });
console.log(toyota);
console.log(toyota.honk());
console.log(toyota.drive());
*/
// ES6
/*
class Car {
constructor({ title }) {
this.title = title;
}
drive() {
return 'vroom';
}
}
class Toyota extends Car {
constructor(options) {
super(options);
this.color = options.color;
}
honk() {
return 'beep';
}
}
const toyota = new Toyota({ color: 'red', title: 'Daily Driver' });
console.log(toyota);
console.log(toyota.drive());
console.log(toyota.honk());
*/
class Monster {
constructor(options) {
this.health = 100;
this.name = options.name;
}
}
class Snake extends Monster {
constructor(options) {
super(options);
}
bite(snake) {
return snake.health -= 10;
}
}
const orm1 = new Snake({ name: 'Cobra'});
const orm2 = new Snake({ name: 'Skallerorm' });
console.log(orm1.bite(orm2));