Private properties in JavaScript

As someone that learned Java at university, then became a full-time PHP code monkey, I've taken property visibility for granted. You don't want people to directly read or write an object's property, no problem! Slap private in front of it and sleep soundly at night knowing you've done a great job.
Well, unless you're working with JavaScript*. But don't worry, there is a way to give you the control you need without too much effort.
Below shows the creation of an employee named John Doe who gets paid 10,000 simoleons (since he lives in SimCity, naturally). The Employee function takes an object with two properties - name and salary , and stores them as local variables. Finally, with a sprinkling of closure magic, it returns a new object with 2 getters and a single setter. These three functions are the only ways to interact with the employee object, meaning direct access to _name and _salary are prohibited.
const Employee = ({ name, salary }) => {
let _name = name;
let _salary = salary;
return {
getName: () => _name,
getSalary: () => _salary,
setSalary: (newSalary) => _salary = newSalary
}
};
const johnDoe = Employee({name: "John Doe", salary: 10000 });
johnDoe.getName(); // "John Doe"
johnDoe.getSalary(); // 10000
johnDoe.setSalary(12000);
johnDoe.getSalary(); // 12000
* No prefixing all property names with a _ won't save you




