Skip to content

Use getters and setters section #180

Description

@DavidVujic

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
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions