function User(fullName) {
if (!~fullName.indexOf(' ')) throw new TypeError('Must be 2 words');
this.fullName = fullName;
Object.defineProperties(this, {
firstName: {
get: function () {
return this.fullName.split(' ')[0];
},
set: function (newFirstName) {
this.fullName = newFirstName + ' ' + this.lastName;
},
configurable: false,
enumerable: true
},
lastName: {
get: function () {
return this.fullName.split(' ')[1];
},
set: function (newLastName) {
this.fullName = this.firstName + ' ' + newLastName;
},
configurable: false,
enumerable: true
}
});
}
let person = new User("Василий Палкин");
// person.fullName = 'It must not work!'