forked from sPredictorX1708/JavaScriptEssentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticMethods.js
More file actions
70 lines (56 loc) · 2.12 KB
/
staticMethods.js
File metadata and controls
70 lines (56 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class HospitalEmployee {
constructor(name) {
this._name = name;
this._remainingVacationDays = 20;
}
get name() {
return this._name;
}
get remainingVacationDays() {
return this._remainingVacationDays;
}
takeVacationDays(daysOff) {
this._remainingVacationDays -= daysOff;
}
static generatePassword() {
return Math.floor(Math.random()*10000)
}
}
class Nurse extends HospitalEmployee {
constructor(name, certifications) {
super(name);
this._certifications = certifications;
}
get certifications() {
return this._certifications;
}
addCertification(newCertification) {
this.certifications.push(newCertification);
}
}
const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);
nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);
//---------------------------------------------------------------------------------------------------------------
//Explaination
// Let’s see how to use the static keyword to create a static method called generateName method in our Animal class:
// class Animal {
// constructor(name) {
// this._name = name;
// this._behavior = 0;
// }
// static generateName() {
// const names = ['Angel', 'Spike', 'Buffy', 'Willow', 'Tara'];
// const randomNumber = Math.floor(Math.random()*5);
// return names[randomNumber];
// }
// }
// In the example above, we create a static method called .generateName() that returns a random name when it’s called. Because of the static keyword, we can only access .generateName() by appending it to the Animal class.
// We call the .generateName() method with the following syntax:
// console.log(Animal.generateName()); // returns a name
// You cannot access the .generateName() method from instances of the Animal class or instances of its subclasses (See below).
// const tyson = new Animal('Tyson');
// tyson.generateName(); // TypeError
// The example above will result in an error, because you cannot call static methods (.generateName()) on an instance (tyson).