How to disable explicitly changing the fullName property?

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("Vasily Palkin");

// person.fullName = 'It must not work!'
June 8th 19 at 17:16
1 answer
June 8th 19 at 17:18
Solution
It would be logical to do the opposite - to set up firstName and lastName, and fullName to do a calculated field.
And use es6 classes.

class User {
 get fullName() {
 return `${this.firstName} ${this.lastName}`;
}
}

Find more questions by tags JavaScript