Accessor Setters in OOP in JavaScript

Now let's make an accessor setter in addition to the getter:

javascriptclass User {
  #name;
  
  get name() {
    return this.#name;
  }
  
  set name(name) {
    this.#name = name;
  }
}

Now let's add a check in the setter:

javascriptclass User {
  #name;
  
  set name(name) {
    if (name.length > 0) {
      this.#name = name;
    } else {
      throw new Error('name is incorrect');
    }
  }
  get name() {
    return this.#name;
  }
}

Let's create an object of the class:

javascriptlet user = new User;

Let's write the data to our property:

javascriptuser.name = 'john';

Let's try to write an incorrect line and get an error:

javascriptuser.name = '';
Задание 1

Implement setters for the properties of class Employee.

Задание 2

Add checks to the setters of the Employee class accessors.