-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path06-es6-classes.js
49 lines (42 loc) · 977 Bytes
/
06-es6-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
/**
* ******************************
* ES6 Classes
* ******************************
**/
/**
* ********************
* Person class
* ********************
**/
class Person {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = new Date(dob);
}
greeting() {
return `Hello there ${this.firstName} ${this.lastName}`;
}
calculateAge() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
getsMarried(newLastName) {
this.lastName = newLastName;
}
static addNumbers(x, y) {
return x + y;
}
}
/**
* ********************
* Create object using a class
* ********************
**/
const mary = new Person('Mary', 'Williams', '11-13-1980');
console.log(mary);
mary.getsMarried('Thompson');
console.log(mary.greeting());
// Static Method
console.log(Person.addNumbers(1, 2));