ES5
var Person = function (name, yearOfBirth, job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
Person.prototype.calculateAge = function () {
var age = new Date().getFullYear - this.yearOfBirth;
console.log(age);
}
var james = new Person('James', 1986, 'Student');
ES6 (ES2015)
class Person {
constructor(name, yearOfBirth, job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
calculateAge() {
var age = new Date().getFullYear - this.yearOfBirth;
console.log(age);
}
static greeting(){
console.log('Hey there!');
}
}
const james = new Person('James', 1986, 'Student');
Static Methods are methods that are attached to the class but are not inherited by objects that are created by the class
static greeting(){
console.log('Hey there!');
}
- Class Definitions are not hoisted, you need to first implement a class and then you can use it.
- Classes have methods not properties