Skip to content

Objects: getters and setters? #670

@erikjung

Description

@erikjung

It seems that getters and setters are absent from the guide. I assume they would be mentioned in close proximity to Objects. Do you ever use them? Do you have any reasons not to?

I have mainly used them for syntax concision, but MDN also mentions this potential benefit of using get:

Getters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed, and if it is never needed, you never pay the cost.

I typically use it in place of methods that require no arguments and would otherwise be prefixed as such:

class Example {
  // bad
  getFoo () {
    return this._foo;
  }

  // good 
  get foo () {
    return this._foo;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions