-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
56 lines (48 loc) · 1.43 KB
/
classes.js
File metadata and controls
56 lines (48 loc) · 1.43 KB
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
// Classes are templates for creating objects.
// They contain methods for working on the data
// Ex. 1
class Surgeon {
constructor(name, department) {
this._name = name;
this._department = department;
this._remainingVacationDays = 20;
}
// note: Class method and getter syntax is the same as it is for objects except you can not include commas between methods.
get name() {
return this._name;
}
get department() {
return this._department;
}
get remainingVacationDays() {
return this._remainingVacationDays;
}
takeVacationDays(daysOff) {
this._remainingVacationDays -= daysOff;
}
}
// create a new instance of Surgeon
const surgeonRomero = new Surgeon('Francisco Romero', 'Cardiovascular');
// access some data
console.log(surgeonRomero.name);
// work on some data
surgeonRomero.takeVacationDays(3);
console.log(surgeonRomero.remainingVacationDays);
// Ex. 2
class Network {
constructor(data, users) {
this.data = data;
this.users = users;
}
// calculate if there's enough bandwidth to watch a movie
movieTime() {
const bandwidth = (this.data - this.users) * 5 >= 10;
return bandwidth;
}
}
// create a new instance of Network
const library = new Network(50, 5);
// view instance (object created)
console.log(library);
// calculate if there's enough bandwidth for movie-time
console.log(library.movieTime());