The "Use getters and setters" section describes that it is hard to enforce the pattern, because JavaScript don't have the public and private keywords. I think this is actually an issue with JavaScript classes. With functions, properties can easily be private. Here's an example (also without the get and set keywords, that I think will cause confusion):
function myObject() {
// this one is private
let myPrivateProperty = 'hello';
// a "getter", made public via the return statement below
const getMyProperty = () => myPrivateProperty;
// a "setter", made public via the return statement below
const setMyProperty = (val) => myPrivateProperty = val;
return {
getMyProperty,
setMyProperty
};
}
The "Use getters and setters" section describes that it is hard to enforce the pattern, because JavaScript don't have the
publicandprivatekeywords. I think this is actually an issue with JavaScript classes. With functions, properties can easily be private. Here's an example (also without thegetandsetkeywords, that I think will cause confusion):