Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
9.1. An Employee class with a private salary
Easy
Topics
This question is in
9. Prototypes and classesA 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
class Employee {
constructor(name, salary)
getSalary() // returns the salary
raise(percent) // returns the new salary
}name: any value, stored as the propertyname.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
new Employee(name, salary)storesnameas a public property, andgetSalary()returnssalary.- The salary is not a property of the object: its only own key is
name,employee.salaryisundefined, andJSON.stringify(employee)shows only the name. - Assigning
employee.salary(oremployee._salary) from outside does not change whatgetSalary()returns. getSalaryandraiseare methods onEmployee.prototype, shared by every employee, not functions copied into each object.- Calling
getSalaryon a look-alike object that was not built byEmployee(for exampleEmployee.prototype.getSalary.call({ salary: 1 })) throws aTypeError. - Each employee has its own salary.
raise(percent)addssalary * percent / 100, rounds the result to cents (two decimal places), stores it, and returns it.raise(0)keeps the salary.raisethrows aRangeErrorwhenpercentis negative or is not a finite number, and the salary stays as it was.- The constructor throws a
TypeErrorwhensalaryis not a finite number (a string,NaN,Infinity,undefined,null) and aRangeErrorwhen it is negative or has a fraction of a cent (1.234). Soraise(0)never changes an accepted salary. - A subclass made with
class Manager extends Employeeandsuper(name, salary)can usegetSalaryandraise.
Examples
const ana = new Employee("Ana", 50000);
ana.getSalary(); // 50000
ana.raise(10); // 55000
ana.salary; // undefined
JSON.stringify(ana); // '{"name":"Ana"}'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.99new Employee("Cy", -1); // RangeError
new Employee("Cy", "1000"); // TypeError
new Employee("Cy", 1.234); // RangeError: not whole cents
Employee.prototype.getSalary.call({ salary: 5 }); // TypeErrorEnvironment
- Plain JavaScript in a browser page; the tests use the global class
Employeefromemployee.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
- Which kind of class member is not a property at all, so
Reflect.ownKeysandJSON.stringifynever see it? - In which order should
raisecheck 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…