Skip to curriculum
Jump to the editor

9.1. An Employee class with a private salary

Easy
Topics

This question is in

9. Prototypes and classes
10–20 min

A payroll screen shows each employee's name to everyone, but the salary only to code that is allowed to see it. If the salary is a normal property, any script can read it, change it, or send it to a server with JSON.stringify. A naming habit like _salary does not stop any of that.

Write the class Employee in employee.js. The name is a public property; the salary lives in a private field, so the only way in or out is through the class's own methods.

Signature

JAVASCRIPT
class Employee {
  constructor(name, salary)
  getSalary()       // returns the salary
  raise(percent)    // returns the new salary
}
  • name: any value, stored as the property name.
  • salary: a finite number of 0 or more, in whole cents (at most two decimal places).
  • percent: a finite number of 0 or more; raise(10) adds 10 %.

Requirements

  1. new Employee(name, salary) stores name as a public property, and getSalary() returns salary.
  2. The salary is not a property of the object: its only own key is name, employee.salary is undefined, and JSON.stringify(employee) shows only the name.
  3. Assigning employee.salary (or employee._salary) from outside does not change what getSalary() returns.
  4. getSalary and raise are methods on Employee.prototype, shared by every employee, not functions copied into each object.
  5. Calling getSalary on a look-alike object that was not built by Employee (for example Employee.prototype.getSalary.call({ salary: 1 })) throws a TypeError.
  6. Each employee has its own salary.
  7. raise(percent) adds salary * percent / 100, rounds the result to cents (two decimal places), stores it, and returns it. raise(0) keeps the salary.
  8. raise throws a RangeError when percent is negative or is not a finite number, and the salary stays as it was.
  9. The constructor throws a TypeError when salary is not a finite number (a string, NaN, Infinity, undefined, null) and a RangeError when it is negative or has a fraction of a cent (1.234). So raise(0) never changes an accepted salary.
  10. A subclass made with class Manager extends Employee and super(name, salary) can use getSalary and raise.

Examples

JAVASCRIPT
const ana = new Employee("Ana", 50000);
ana.getSalary();          // 50000
ana.raise(10);            // 55000
ana.salary;               // undefined
JSON.stringify(ana);      // '{"name":"Ana"}'
JAVASCRIPT
const ben = new Employee("Ben", 999.99);
ben.raise(3.3);           // 1032.99 (999.99 * 1.033 = 1032.98967, rounded to cents)
ben.salary = 1;           // adds an unrelated public property
ben.getSalary();          // 1032.99
JAVASCRIPT
new Employee("Cy", -1);                        // RangeError
new Employee("Cy", "1000");                    // TypeError
new Employee("Cy", 1.234);                     // RangeError: not whole cents
Employee.prototype.getSalary.call({ salary: 5 }); // TypeError

Environment

  • Plain JavaScript in a browser page; the tests use the global class Employee from employee.js.
  • Use a real private field. A closure in the constructor would hide the salary too, but it puts the methods on every object (requirement 4).

Hints

  1. Which kind of class member is not a property at all, so Reflect.ownKeys and JSON.stringify never see it?
  2. In which order should raise check its argument and change the salary, so a bad call leaves nothing half-done?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…