ES6 Classes
class Person {
constructor(firstname, lastname, birthYear) {
this.firstname = firstname;
this.lastname = lastname;
this.birthYear = birthYear;
}
calcAge() {
const currentYear = new Date().getFullYear();
console.log(currentYear - this.birthYear);
}
}
const jessica = new Person('Jessica', 'Doe', 1990);Getters and Setters
class Person {
constructor(fullName, birthYear) {
this.fullName = fullName;
this.birthYear = birthYear;
}
calcAge() {
const currentYear = new Date().getFullYear();
console.log(currentYear - this.birthYear);
}
get age() {
return new Date().getFullYear() - this.birthYear;
}
// Set a property that already exists
get fullName() {
return this._fullName;
}
set fullName(name) {
if (name.includes(' ')) this._fullName = name;
else console.log('Please provide a full name with a first and last name.');
}
}
const jessica = new Person('Jessica Davies', 'Doe', 1990);
jessica.fullName = 'Kevin Teaser';Statische Methoden
Vererbung
Encapsulation
Last updated