Sobes.tech
Junior — Middle

Do you use methods to get property values in your classes?

sobes.tech AI

Answer from AI

Yes, in Node.js, methods for obtaining property values of classes are often used, especially through getters (get). This allows controlling access to properties, performing additional logic when reading the value, and improving encapsulation.

Example of using a getter in a class:

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

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

const user = new User('Alice');
console.log(user.name); // Outputs 'ALICE'

In this example, accessing user.name calls the get name() method, which returns the name in uppercase.