Sobes.tech
Junior — Middle

Can you explain what a heter is in object-oriented programming?

sobes.tech AI

Answer from AI

In object-oriented programming, the term "getter" refers to a method used to retrieve the value of a private field of an object. This is part of encapsulation — a principle that hides the internal data of an object and provides access to it through methods.

For example, if a class has a field name, the getter would look like this:

class Person {
  constructor(name) {
    this._name = name;
  }

  getName() {
    return this._name;
  }
}

const p = new Person('Ivan');
console.log(p.getName()); // Ivan

In modern versions of JavaScript, you can use getters with the get keyword:

class Person {
  constructor(name) {
    this._name = name;
  }

  get name() {
    return this._name;
  }
}

const p = new Person('Ivan');
console.log(p.name); // Ivan

Thus, a getter allows controlling access to the internal data of an object and adding logic when retrieving it if necessary.

Can you explain what a heter is in object-oriented… - sobes.tech