-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticMethods.js
More file actions
54 lines (43 loc) · 1.1 KB
/
staticMethods.js
File metadata and controls
54 lines (43 loc) · 1.1 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
class Person {
constructor(firstName, birthYear) {
this.firstName = firstName;
this.birthYear = birthYear;
}
// Instance methods
calcAge() {
console.log(2037 - this.birthYear);
}
get age() {
console.log(2023 - this.birthYear);
}
// Not an instance - static method
static hey() {
console.log('Hello there!');
console.log(this);
}
}
// Array.from(); // convert Array structure to real array;
// It can only be used on the Array constructor and not the prototype
// Person.hey();
// Create a student class.
class Student extends Person {
constructor(firstName, birthYear, course) {
super(firstName, birthYear);
this.course = course;
}
introduce() {
console.log(`Hi, I'm ${this.firstName} and I'm studying ${this.course}`);
}
calcAge() {
console.log(
`I'm ${2037 - this.birthYear} years old, but I feel more like ${
2037 - this.birthYear - 5
}`
);
}
}
Student.prototype = Object.create(Person.prototype);
const martha = new Student('Martha', 1991, 'Computer Science');
console.log(martha);
martha.introduce();
martha.calcAge();