class Person { constructor (firstName, lastName, dateOfBirth) { this._firstName = firstName; this._lastName = lastName; // expected to be an instance of Date class this._dateOfBirth = dateOfBirth; } set firstName (newValue) { this._firstName = newValue; } get firstName() { return this._firstName; } set lastName (newValue) { this._lastName = newValue; } get lastName () { return this._lastName; } get fullName () { return this._firstName + ' ' + this._lastName; } set fullName (newValue) { console.error('You cannot set the fullName, change firstName and lastName seperately instead.'); } // the age calculation will fail, if dateOfBirth won't contain a Date instance get age() { // get the current Date let currentDate = new Date(); // split start date components let syear = this._dateOfBirth.getYear(); let smonth = this._dateOfBirth.getMonth(); let sday = this._dateOfBirth.getDate(); // split end date components let eyear = currentDate.getYear(); let emonth = currentDate.getMonth(); let eday = currentDate.getDate(); // normalize day of month to 30, if bigger if (sday === 31) sday = 30; if (eday === 31) eday = 30; // (don't change February 28 or February 29) // linearize the components to a harmonized value range let startDateLinearized = (eday + emonth * 30 + eyear * 360); let endDateLinearized = (sday + smonth * 30 + syear * 360); // return the difference fraction without the fraction part return Math.floor((startDateLinearized - endDateLinearized) / 360); } set age(newValue) { console.error('You cannot set the age, its calculated, change dateOfBirth instead.'); } } var andre = new Person('André','Kleinschmidt',new Date('1981-11-27')); console.log(andre.firstName + ' is ' + andre.age + ' years old.');