-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
89 lines (66 loc) · 1.49 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
85
86
87
88
89
/*jshint esversion: 6 */
// Classes
//ES5
function Car(options) {
this.title = options.title;
}
Car.prototype.drive = function() {
return "Wroom";
};
function Toyota(options) {
// Get the property options from Car
Car.call(this, options);
this.color = options.color;
}
// Get the methods from Car
Toyota.prototype = Object.create(Car.prototype);
Toyota.prototype.construct = Toyota;
// Setup own method
Toyota.prototype.honk = function() {
return "Tööt";
};
const car = new Car({title: "Focus"});
const toyota = new Toyota({color: "red", title: "Daily driver"});
console.log(toyota.drive());
console.log(toyota.honk());
// ES6
class Car6 {
constructor({ title }) {
this.title = title;
}
drive() {
return "Wroom6";
}
}
class Toyota6 extends Car6 {
constructor(options) {
// Run the constructor of Car6
super(options);
this.color = options.color;
this.running = false;
}
honk() {
// This would run the honk() of Car6,
// if it has that method
//super();
return "Tööt6";
}
// Getters and Setter
get Running() {
return this.running;
}
set Running(start) {
this.running = start;
console.log("The value has been set.");
}
}
const car6 = new Car6({ title: "Toyota6"});
const toyota6 = new Toyota6({ color: "red", title: "MyCar" });
console.log(car6);
console.log(toyota6);
// Using getters and setter
console.log(toyota6.Running);
toyota6.Running = true;
console.log(toyota6.Running);
console.log(toyota6);
// Use case