Advertisement
JPDG13

Classes in ES6

Apr 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Classes in ES6
  2.  
  3. // Classes in ES6 are not hoisted. Also, you can only add methods to classes, not properties.
  4. class Person {
  5.     // Every class must have a constructor
  6.     constructor(name, birthYear, job) {
  7.         this.name = name;
  8.         this.birthYear = birthYear;
  9.         this.job = job;
  10.     }   // no comma here, oddly enough.
  11.     //Methods are added here
  12.     calculateAge() {
  13.         var age = new Date().getFullYear - this.birthYear;
  14.         console.log(age);
  15.     }
  16.     // static class example
  17.     static greetings() {
  18.         console.log('Hey there!');
  19.     }
  20. }
  21.  
  22. const john6 = new Person ('John', 1990, 'teacher');
  23. Person.greetings(); //static class must be invoked.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement