Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. //ES6 Classes (syntactic sugar built upon protoypes)
  2.  
  3. class Person {
  4. constructor(firstName, lastName, dob){
  5. this.firstName = firstName;
  6. this.lastName = lastName;
  7. this.birthday = new Date(dob);
  8. }
  9. greeting(){
  10. return `Hello there ${this.firstName} ${this.lastName}`;
  11. }
  12.  
  13. calculateAge(){
  14. const diff = Date.now() - this.birthday.getTime();
  15. const ageDate = new Date(diff);
  16. return Math.abs(ageDate.getUTCFullYear() - 1970);
  17. }
  18.  
  19. getsMarried(newLastName){
  20. this.lastName = newLastName;
  21. }
  22.  
  23. static addNumbers(x, y){
  24. return x + y;
  25. }
  26. }
  27.  
  28. const mary = new Person('Mary', 'Williams', '11-13-1980');
  29.  
  30. mary.getsMarried('Thompson');
  31.  
  32. console.log(mary);
  33. console.log(mary.greeting());
  34. console.log(mary.calculateAge());
  35.  
  36. console.log(Person.addNumbers(1,2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement