kirillurgant
3/19/2017 - 2:46 PM

descriptors_getters_setters.js

function User(fullName) {
  this.fullName = fullName;
  
  Object.defineProperties(this, {
    firstName: {
      get: function() {
        return this.fullName.split(" ")[0]
      },
      set: function(val) {
        this.fullName = val + " " + this.lastName;
      }
    },
    lastName: {
      get: function() {
        return this.fullName.split(" ")[1]
      },
      set: function(val) {
        this.fullName = this.firstName + " " + val;
      }
    }
  })
}

var vasya = new User("Василий Попкин");
console.log(vasya.lastName);
console.log(vasya.firstName);

vasya.firstName = "Игорь";
console.log(vasya.fullName);
vasya.lastName = "Иванов";
console.log(vasya.fullName);